Blame SOURCES/expat-2.2.5-Prevent-integer-overflow-on-m_groupSize-in-function.patch

83eb0d
commit 835df27bc1a1eae1ec51b14122ea40c974dd7409
83eb0d
Author: Tomas Korbar <tkorbar@redhat.com>
83eb0d
Date:   Mon Feb 14 12:29:20 2022 +0100
83eb0d
83eb0d
    CVE-2021-46143
83eb0d
83eb0d
diff --git a/lib/xmlparse.c b/lib/xmlparse.c
83eb0d
index c45be0c..22d0a75 100644
83eb0d
--- a/lib/xmlparse.c
83eb0d
+++ b/lib/xmlparse.c
83eb0d
@@ -4995,6 +4995,11 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end,
83eb0d
     case XML_ROLE_GROUP_OPEN:
83eb0d
       if (parser->m_prologState.level >= parser->m_groupSize) {
83eb0d
         if (parser->m_groupSize) {
83eb0d
+          /* Detect and prevent integer overflow */
83eb0d
+          if (parser->m_groupSize > (unsigned int)(-1) / 2u) {
83eb0d
+            return XML_ERROR_NO_MEMORY;
83eb0d
+          }
83eb0d
+
83eb0d
           char *temp = (char *)REALLOC(parser, parser->m_groupConnector, parser->m_groupSize *= 2);
83eb0d
           if (temp == NULL) {
83eb0d
             parser->m_groupSize /= 2;
83eb0d
@@ -5002,6 +5007,15 @@ doProlog(XML_Parser parser, const ENCODING *enc, const char *s, const char *end,
83eb0d
           }
83eb0d
           parser->m_groupConnector = temp;
83eb0d
           if (dtd->scaffIndex) {
83eb0d
+            /* Detect and prevent integer overflow.
83eb0d
+             * The preprocessor guard addresses the "always false" warning
83eb0d
+             * from -Wtype-limits on platforms where
83eb0d
+             * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */
83eb0d
+#if UINT_MAX >= SIZE_MAX
83eb0d
+            if (parser->m_groupSize > (size_t)(-1) / sizeof(int)) {
83eb0d
+              return XML_ERROR_NO_MEMORY;
83eb0d
+            }
83eb0d
+#endif
83eb0d
             int *temp = (int *)REALLOC(parser, dtd->scaffIndex,
83eb0d
                           parser->m_groupSize * sizeof(int));
83eb0d
             if (temp == NULL)