1- /* 60e137abb91af642d6c3988f8f133d23329b32638659c74d47125fc0faf6ddd5 (2.7.2 +)
1+ /* 28bcd8b1ba7eb595d82822908257fd9c3589b4243e3c922d0369f35bfcd7b506 (2.7.3 +)
22 __ __ _
33 ___\ \/ /_ __ __ _| |_
44 / _ \\ /| '_ \ / _` | __|
4141 Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow <[email protected] > 4242 Copyright (c) 2024-2025 Berkay Eren Ürün <[email protected] > 4343 Copyright (c) 2024 Hanno Böck <[email protected] > 44+ Copyright (c) 2025 Matthew Fernandez <[email protected] > 4445 Licensed under the MIT license:
4546
4647 Permission is hereby granted, free of charge, to any person obtaining
@@ -850,14 +851,14 @@ static void *
850851# endif
851852expat_malloc (XML_Parser parser , size_t size , int sourceLine ) {
852853 // Detect integer overflow
853- if (SIZE_MAX - size < sizeof (size_t )) {
854+ if (SIZE_MAX - size < sizeof (size_t ) + EXPAT_MALLOC_PADDING ) {
854855 return NULL ;
855856 }
856857
857858 const XML_Parser rootParser = getRootParserOf (parser , NULL );
858859 assert (rootParser -> m_parentParser == NULL );
859860
860- const size_t bytesToAllocate = sizeof (size_t ) + size ;
861+ const size_t bytesToAllocate = sizeof (size_t ) + EXPAT_MALLOC_PADDING + size ;
861862
862863 if ((XmlBigCount )- 1 - rootParser -> m_alloc_tracker .bytesAllocated
863864 < bytesToAllocate ) {
@@ -894,7 +895,7 @@ expat_malloc(XML_Parser parser, size_t size, int sourceLine) {
894895 rootParser -> m_alloc_tracker .peakBytesAllocated , sourceLine );
895896 }
896897
897- return (char * )mallocedPtr + sizeof (size_t );
898+ return (char * )mallocedPtr + sizeof (size_t ) + EXPAT_MALLOC_PADDING ;
898899}
899900
900901# if defined(XML_TESTING )
@@ -914,8 +915,9 @@ expat_free(XML_Parser parser, void *ptr, int sourceLine) {
914915
915916 // Extract size (to the eyes of malloc_fcn/realloc_fcn) and
916917 // the original pointer returned by malloc/realloc
917- void * const mallocedPtr = (char * )ptr - sizeof (size_t );
918- const size_t bytesAllocated = sizeof (size_t ) + * (size_t * )mallocedPtr ;
918+ void * const mallocedPtr = (char * )ptr - EXPAT_MALLOC_PADDING - sizeof (size_t );
919+ const size_t bytesAllocated
920+ = sizeof (size_t ) + EXPAT_MALLOC_PADDING + * (size_t * )mallocedPtr ;
919921
920922 // Update accounting
921923 assert (rootParser -> m_alloc_tracker .bytesAllocated >= bytesAllocated );
@@ -954,7 +956,7 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
954956
955957 // Extract original size (to the eyes of the caller) and the original
956958 // pointer returned by malloc/realloc
957- void * mallocedPtr = (char * )ptr - sizeof (size_t );
959+ void * mallocedPtr = (char * )ptr - EXPAT_MALLOC_PADDING - sizeof (size_t );
958960 const size_t prevSize = * (size_t * )mallocedPtr ;
959961
960962 // Classify upcoming change
@@ -969,8 +971,13 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
969971 }
970972 }
971973
974+ // NOTE: Integer overflow detection has already been done for us
975+ // by expat_heap_increase_tolerable(..) above
976+ assert (SIZE_MAX - sizeof (size_t ) - EXPAT_MALLOC_PADDING >= size );
977+
972978 // Actually allocate
973- mallocedPtr = parser -> m_mem .realloc_fcn (mallocedPtr , sizeof (size_t ) + size );
979+ mallocedPtr = parser -> m_mem .realloc_fcn (
980+ mallocedPtr , sizeof (size_t ) + EXPAT_MALLOC_PADDING + size );
974981
975982 if (mallocedPtr == NULL ) {
976983 return NULL ;
@@ -1001,7 +1008,7 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
10011008 // Update in-block recorded size
10021009 * (size_t * )mallocedPtr = size ;
10031010
1004- return (char * )mallocedPtr + sizeof (size_t );
1011+ return (char * )mallocedPtr + sizeof (size_t ) + EXPAT_MALLOC_PADDING ;
10051012}
10061013#endif // XML_GE == 1
10071014
@@ -1337,7 +1344,8 @@ parserCreate(const XML_Char *encodingName,
13371344 XML_Parser parser = NULL ;
13381345
13391346#if XML_GE == 1
1340- const size_t increase = sizeof (size_t ) + sizeof (struct XML_ParserStruct );
1347+ const size_t increase
1348+ = sizeof (size_t ) + EXPAT_MALLOC_PADDING + sizeof (struct XML_ParserStruct );
13411349
13421350 if (parentParser != NULL ) {
13431351 const XML_Parser rootParser = getRootParserOf (parentParser , NULL );
@@ -1352,11 +1360,13 @@ parserCreate(const XML_Char *encodingName,
13521360 if (memsuite ) {
13531361 XML_Memory_Handling_Suite * mtemp ;
13541362#if XML_GE == 1
1355- void * const sizeAndParser = memsuite -> malloc_fcn (
1356- sizeof (size_t ) + sizeof (struct XML_ParserStruct ));
1363+ void * const sizeAndParser
1364+ = memsuite -> malloc_fcn (sizeof (size_t ) + EXPAT_MALLOC_PADDING
1365+ + sizeof (struct XML_ParserStruct ));
13571366 if (sizeAndParser != NULL ) {
13581367 * (size_t * )sizeAndParser = sizeof (struct XML_ParserStruct );
1359- parser = (XML_Parser )((char * )sizeAndParser + sizeof (size_t ));
1368+ parser = (XML_Parser )((char * )sizeAndParser + sizeof (size_t )
1369+ + EXPAT_MALLOC_PADDING );
13601370#else
13611371 parser = memsuite -> malloc_fcn (sizeof (struct XML_ParserStruct ));
13621372 if (parser != NULL ) {
@@ -1369,11 +1379,12 @@ parserCreate(const XML_Char *encodingName,
13691379 } else {
13701380 XML_Memory_Handling_Suite * mtemp ;
13711381#if XML_GE == 1
1372- void * const sizeAndParser
1373- = malloc ( sizeof ( size_t ) + sizeof (struct XML_ParserStruct ));
1382+ void * const sizeAndParser = malloc ( sizeof ( size_t ) + EXPAT_MALLOC_PADDING
1383+ + sizeof (struct XML_ParserStruct ));
13741384 if (sizeAndParser != NULL ) {
13751385 * (size_t * )sizeAndParser = sizeof (struct XML_ParserStruct );
1376- parser = (XML_Parser )((char * )sizeAndParser + sizeof (size_t ));
1386+ parser = (XML_Parser )((char * )sizeAndParser + sizeof (size_t )
1387+ + EXPAT_MALLOC_PADDING );
13771388#else
13781389 parser = malloc (sizeof (struct XML_ParserStruct ));
13791390 if (parser != NULL ) {
@@ -6437,6 +6448,10 @@ internalEntityProcessor(XML_Parser parser, const char *s, const char *end,
64376448 // process its possible inner entities (which are added to the
64386449 // m_openInternalEntities during doProlog or doContent calls above)
64396450 entity -> hasMore = XML_FALSE ;
6451+ if (! entity -> is_param
6452+ && (openEntity -> startTagLevel != parser -> m_tagLevel )) {
6453+ return XML_ERROR_ASYNC_ENTITY ;
6454+ }
64406455 triggerReenter (parser );
64416456 return result ;
64426457 } // End of entity processing, "if" block will return here
@@ -8135,7 +8150,7 @@ poolGrow(STRING_POOL *pool) {
81358150 if (bytesToAllocate == 0 )
81368151 return XML_FALSE ;
81378152
8138- temp = REALLOC (pool -> parser , pool -> blocks , ( unsigned ) bytesToAllocate );
8153+ temp = REALLOC (pool -> parser , pool -> blocks , bytesToAllocate );
81398154 if (temp == NULL )
81408155 return XML_FALSE ;
81418156 pool -> blocks = temp ;
0 commit comments