From 76fe08d97de88bfaef2f7d5cd27f11954cc5bee2 Mon Sep 17 00:00:00 2001
From: Hieu Le Minh <leminhhieu.opensource@gmail.com>
Date: Sat, 18 Apr 2026 21:18:24 +0700
Subject: [PATCH] xmlregexp: Prevent out-of-bounds read in NXT macro

Fixes: https://gitlab.gnome.org/GNOME/libxml2/-/issues/1099

Backport of upstream commit 76fe08d97de88bfaef2f7d5cd27f11954cc5bee2 to
2.13.8. CVE-2026-86137 / bsc#1279963.

Adapted for 2.13.8: the xmlRegNewParserCtxt change adds the ret->len
assignment before the existing if (ret->string == NULL) check. The NXT
macro and struct hunks are regenerated for this tree, because the
upstream 2.15.x context does not match.
--- a/xmlregexp.c
index 3fe20ac0d..ad3b3dd3c 100644
+++ b/xmlregexp.c
@@ -46,7 +46,9 @@
     xmlRegexpErrCompile(ctxt, str);
 #define NEXT ctxt->cur++
 #define CUR (*(ctxt->cur))
-#define NXT(index) (ctxt->cur[index])
+#define NXT(index)									\
+    (((size_t)(ctxt->cur + index - ctxt->string) < ctxt->len)				\
+      ? ctxt->cur[index] : 0)
 
 #define NEXTL(l) ctxt->cur += l;
 #define XML_REG_STRING_SEPARATOR '|'
@@ -225,6 +227,7 @@
 struct _xmlAutomata {
     xmlChar *string;
     xmlChar *cur;
+    size_t len;
 
     int error;
     int neg;
@@ -679,6 +682,7 @@
     memset(ret, 0, sizeof(xmlRegParserCtxt));
     if (string != NULL) {
 	ret->string = xmlStrdup(string);
+	ret->len = strlen((const char *) ret->string);
         if (ret->string == NULL) {
             xmlFree(ret);
             return(NULL);

