From 0cb3d4d101334a2b8728f810dd14e71af62c2dc9 Mon Sep 17 00:00:00 2001 From: lvfei Date: Thu, 20 Jul 2023 15:11:53 +0800 Subject: [PATCH] CVE-2021-46143 --- parser/expat/lib/xmlparse.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/parser/expat/lib/xmlparse.c b/parser/expat/lib/xmlparse.c index f81a68d2fc..d087eb48da 100644 --- a/parser/expat/lib/xmlparse.c +++ b/parser/expat/lib/xmlparse.c @@ -5046,11 +5046,26 @@ doProlog(XML_Parser parser, case XML_ROLE_GROUP_OPEN: if (prologState.level >= groupSize) { if (groupSize) { + + /* Detect and prevent integer overflow */ + if (parser->m_groupSize > (unsigned int)(-1) / 2u) { + return XML_ERROR_NO_MEMORY; + } + char *temp = (char *)REALLOC(groupConnector, groupSize *= 2); if (temp == NULL) return XML_ERROR_NO_MEMORY; groupConnector = temp; if (dtd->scaffIndex) { + /* Detect and prevent integer overflow. + * The preprocessor guard addresses the "always false" warning + * from -Wtype-limits on platforms where + * sizeof(unsigned int) < sizeof(size_t), e.g. on x86_64. */ +#if UINT_MAX >= SIZE_MAX + if (parser->m_groupSize > (size_t)(-1) / sizeof(int)) { + return XML_ERROR_NO_MEMORY; + } +#endif int *temp = (int *)REALLOC(dtd->scaffIndex, groupSize * sizeof(int)); if (temp == NULL) -- 2.27.0