From 90f293ba74d28b1d570920382e707586f68ebf35 Mon Sep 17 00:00 00:00 +0000
From: Daniel Garcia Moreno <daniel.garcia@suse.com>
Date: Mon, 4 May 2026 09:54:34 +0200
Subject: [PATCH] xmlIO: Check for int overflow before calling writecallback

Fix https://gitlab.gnome.org/GNOME/libxml2/-/work_items/1111

Backport of upstream commit 90f293ba74d28b1d570920382e707586f68ebf35 to
2.13.8. CVE-2026-86143 / bsc#1279969.

Adapted for 2.13.8: the xmlOutputBufferWrite hunk is applied at the two
nbchars assignment sites instead of before the writecallback call,
because pre-2.15 trees declare nbchars as int, so the size_t to int
coercion of xmlBufUse() happens at the assignment. The same guard is
added at the two nbchars assignment sites in xmlOutputBufferWriteEscape,
which upstream restructured in 2.15.x and therefore the commit does not
touch. The xmlOutputBufferFlush hunk is unchanged.
--- a/xmlIO.c
index 0000000000..0000000000 100644
+++ b/xmlIO.c
@@ -2512,9 +2512,14 @@ xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
 	    ret = xmlCharEncOutput(out, 0);
 	    if (ret < 0)
 		return(-1);
-            if (out->writecallback)
-	        nbchars = xmlBufUse(out->conv);
-            else
+            if (out->writecallback) {
+                size_t bufsize = xmlBufUse(out->conv);
+                if (bufsize >= INT_MAX) {
+                    out->error = XML_ERR_INTERNAL_ERROR;
+                    return(-1);
+                }
+                nbchars = bufsize;
+            } else
                 nbchars = ret >= 0 ? ret : 0;
 	} else {
 	    ret = xmlBufAdd(out->buffer, (const xmlChar *) buf, chunk);
@@ -2522,9 +2527,14 @@ xmlOutputBufferWrite(xmlOutputBufferPtr out, int len, const char *buf) {
                 out->error = XML_ERR_NO_MEMORY;
 	        return(-1);
             }
-            if (out->writecallback)
-	        nbchars = xmlBufUse(out->buffer);
-            else
+            if (out->writecallback) {
+                size_t bufsize = xmlBufUse(out->buffer);
+                if (bufsize >= INT_MAX) {
+                    out->error = XML_ERR_INTERNAL_ERROR;
+                    return(-1);
+                }
+                nbchars = bufsize;
+            } else
                 nbchars = chunk;
 	}
 	buf += chunk;
@@ -2714,9 +2724,14 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
 	    ret = xmlCharEncOutput(out, 0);
 	    if (ret < 0)
 		return(-1);
-            if (out->writecallback)
-	        nbchars = xmlBufUse(out->conv);
-            else
+            if (out->writecallback) {
+                size_t bufsize = xmlBufUse(out->conv);
+                if (bufsize >= INT_MAX) {
+                    out->error = XML_ERR_INTERNAL_ERROR;
+                    return(-1);
+                }
+                nbchars = bufsize;
+            } else
                 nbchars = ret >= 0 ? ret : 0;
 	} else {
 	    ret = escaping(xmlBufEnd(out->buffer), &chunk, str, &cons);
@@ -2725,9 +2740,14 @@ xmlOutputBufferWriteEscape(xmlOutputBufferPtr out, const xmlChar *str,
                 return(-1);
             }
             xmlBufAddLen(out->buffer, chunk);
-            if (out->writecallback)
-	        nbchars = xmlBufUse(out->buffer);
-            else
+            if (out->writecallback) {
+                size_t bufsize = xmlBufUse(out->buffer);
+                if (bufsize >= INT_MAX) {
+                    out->error = XML_ERR_INTERNAL_ERROR;
+                    return(-1);
+                }
+                nbchars = bufsize;
+            } else
                 nbchars = chunk;
 	}
 	str += cons;
@@ -2883,15 +2903,25 @@ xmlOutputBufferFlush(xmlOutputBufferPtr out) {
      */
     if ((out->conv != NULL) && (out->encoder != NULL) &&
 	(out->writecallback != NULL)) {
+        size_t bufsize = xmlBufUse(out->conv);
+        if (bufsize >= INT_MAX) {
+            out->error = XML_ERR_INTERNAL_ERROR;
+            return(-1);
+        }
 	ret = out->writecallback(out->context,
                                  (const char *)xmlBufContent(out->conv),
-                                 xmlBufUse(out->conv));
+                                 bufsize);
 	if (ret >= 0)
 	    xmlBufShrink(out->conv, ret);
     } else if (out->writecallback != NULL) {
+        size_t bufsize = xmlBufUse(out->buffer);
+        if (bufsize >= INT_MAX) {
+            out->error = XML_ERR_INTERNAL_ERROR;
+            return(-1);
+        }
 	ret = out->writecallback(out->context,
                                  (const char *)xmlBufContent(out->buffer),
-                                 xmlBufUse(out->buffer));
+                                 bufsize);
 	if (ret >= 0)
 	    xmlBufShrink(out->buffer, ret);
     }
