Blame SOURCES/expat-2.2.10-Detect-and-prevent-integer-overflow-in-XML_GetBuffer.patch

463a2a
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
463a2a
index d54af683..5ce31402 100644
463a2a
--- a/lib/xmlparse.c
463a2a
+++ b/lib/xmlparse.c
463a2a
@@ -2067,6 +2067,11 @@ XML_GetBuffer(XML_Parser parser, int len) {
463a2a
     keep = (int)EXPAT_SAFE_PTR_DIFF(parser->m_bufferPtr, parser->m_buffer);
463a2a
     if (keep > XML_CONTEXT_BYTES)
463a2a
       keep = XML_CONTEXT_BYTES;
463a2a
+    /* Detect and prevent integer overflow */
463a2a
+    if (keep > INT_MAX - neededSize) {
463a2a
+      parser->m_errorCode = XML_ERROR_NO_MEMORY;
463a2a
+      return NULL;
463a2a
+    }
463a2a
     neededSize += keep;
463a2a
 #endif /* defined XML_CONTEXT_BYTES */
463a2a
     if (neededSize
463a2a
diff --git a/tests/runtests.c b/tests/runtests.c
463a2a
index e89e8220..579dad1a 100644
463a2a
--- a/tests/runtests.c
463a2a
+++ b/tests/runtests.c
463a2a
@@ -3847,6 +3847,30 @@ START_TEST(test_get_buffer_2) {
463a2a
 }
463a2a
 END_TEST
463a2a
 
463a2a
+/* Test for signed integer overflow CVE-2022-23852 */
463a2a
+#if defined(XML_CONTEXT_BYTES)
463a2a
+START_TEST(test_get_buffer_3_overflow) {
463a2a
+  XML_Parser parser = XML_ParserCreate(NULL);
463a2a
+  assert(parser != NULL);
463a2a
+
463a2a
+  const char *const text = "\n";
463a2a
+  const int expectedKeepValue = (int)strlen(text);
463a2a
+
463a2a
+  // After this call, variable "keep" in XML_GetBuffer will
463a2a
+  // have value expectedKeepValue
463a2a
+  if (XML_Parse(parser, text, (int)strlen(text), XML_FALSE /* isFinal */)
463a2a
+      == XML_STATUS_ERROR)
463a2a
+    xml_failure(parser);
463a2a
+
463a2a
+  assert(expectedKeepValue > 0);
463a2a
+  if (XML_GetBuffer(parser, INT_MAX - expectedKeepValue + 1) != NULL)
463a2a
+    fail("enlarging buffer not failed");
463a2a
+
463a2a
+  XML_ParserFree(parser);
463a2a
+}
463a2a
+END_TEST
463a2a
+#endif // defined(XML_CONTEXT_BYTES)
463a2a
+
463a2a
 /* Test position information macros */
463a2a
 START_TEST(test_byte_info_at_end) {
463a2a
   const char *text = "<doc></doc>";
463a2a
@@ -11731,6 +11755,9 @@ make_suite(void) {
463a2a
   tcase_add_test(tc_basic, test_empty_parse);
463a2a
   tcase_add_test(tc_basic, test_get_buffer_1);
463a2a
   tcase_add_test(tc_basic, test_get_buffer_2);
463a2a
+#if defined(XML_CONTEXT_BYTES)
463a2a
+  tcase_add_test(tc_basic, test_get_buffer_3_overflow);
463a2a
+#endif
463a2a
   tcase_add_test(tc_basic, test_byte_info_at_end);
463a2a
   tcase_add_test(tc_basic, test_byte_info_at_error);
463a2a
   tcase_add_test(tc_basic, test_byte_info_at_cdata);
463a2a