gh-139312: Update bundled libexpat to 2.7.3 (GH-139319) · hartwork/cpython@48d0d0d (original) (raw)
1
``
`-
/* 60e137abb91af642d6c3988f8f133d23329b32638659c74d47125fc0faf6ddd5 (2.7.2+)
`
``
1
`+
/* 28bcd8b1ba7eb595d82822908257fd9c3589b4243e3c922d0369f35bfcd7b506 (2.7.3+)
`
2
2
` __ __ _
`
3
3
` __\ / / __ __ | |
`
4
4
`` / _ \ /| '_ \ / _` | __|
``
41
41
` Copyright (c) 2023-2024 Sony Corporation / Snild Dolkow snild@sony.com
`
42
42
` Copyright (c) 2024-2025 Berkay Eren Ürün berkay.ueruen@siemens.com
`
43
43
` Copyright (c) 2024 Hanno Böck hanno@gentoo.org
`
``
44
`+
Copyright (c) 2025 Matthew Fernandez matthew.fernandez@gmail.com
`
44
45
` Licensed under the MIT license:
`
45
46
``
46
47
` Permission is hereby granted, free of charge, to any person obtaining
`
`@@ -850,14 +851,14 @@ static void *
`
850
851
`# endif
`
851
852
`expat_malloc(XML_Parser parser, size_t size, int sourceLine) {
`
852
853
`// Detect integer overflow
`
853
``
`-
if (SIZE_MAX - size < sizeof(size_t)) {
`
``
854
`+
if (SIZE_MAX - size < sizeof(size_t) + EXPAT_MALLOC_PADDING) {
`
854
855
`return NULL;
`
855
856
` }
`
856
857
``
857
858
`const XML_Parser rootParser = getRootParserOf(parser, NULL);
`
858
859
`assert(rootParser->m_parentParser == NULL);
`
859
860
``
860
``
`-
const size_t bytesToAllocate = sizeof(size_t) + size;
`
``
861
`+
const size_t bytesToAllocate = sizeof(size_t) + EXPAT_MALLOC_PADDING + size;
`
861
862
``
862
863
`if ((XmlBigCount)-1 - rootParser->m_alloc_tracker.bytesAllocated
`
863
864
`< bytesToAllocate) {
`
`@@ -894,7 +895,7 @@ expat_malloc(XML_Parser parser, size_t size, int sourceLine) {
`
894
895
`rootParser->m_alloc_tracker.peakBytesAllocated, sourceLine);
`
895
896
` }
`
896
897
``
897
``
`-
return (char *)mallocedPtr + sizeof(size_t);
`
``
898
`+
return (char *)mallocedPtr + sizeof(size_t) + EXPAT_MALLOC_PADDING;
`
898
899
`}
`
899
900
``
900
901
`# if defined(XML_TESTING)
`
`@@ -914,8 +915,9 @@ expat_free(XML_Parser parser, void *ptr, int sourceLine) {
`
914
915
``
915
916
`// Extract size (to the eyes of malloc_fcn/realloc_fcn) and
`
916
917
`// 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;
`
919
921
``
920
922
`// Update accounting
`
921
923
`assert(rootParser->m_alloc_tracker.bytesAllocated >= bytesAllocated);
`
`@@ -954,7 +956,7 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
`
954
956
``
955
957
`// Extract original size (to the eyes of the caller) and the original
`
956
958
`// pointer returned by malloc/realloc
`
957
``
`-
void *mallocedPtr = (char *)ptr - sizeof(size_t);
`
``
959
`+
void *mallocedPtr = (char *)ptr - EXPAT_MALLOC_PADDING - sizeof(size_t);
`
958
960
`const size_t prevSize = *(size_t *)mallocedPtr;
`
959
961
``
960
962
`// Classify upcoming change
`
`@@ -969,8 +971,13 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
`
969
971
` }
`
970
972
` }
`
971
973
``
``
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
+
972
978
`// 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);
`
974
981
``
975
982
`if (mallocedPtr == NULL) {
`
976
983
`return NULL;
`
`@@ -1001,7 +1008,7 @@ expat_realloc(XML_Parser parser, void *ptr, size_t size, int sourceLine) {
`
1001
1008
`// Update in-block recorded size
`
1002
1009
`*(size_t *)mallocedPtr = size;
`
1003
1010
``
1004
``
`-
return (char *)mallocedPtr + sizeof(size_t);
`
``
1011
`+
return (char *)mallocedPtr + sizeof(size_t) + EXPAT_MALLOC_PADDING;
`
1005
1012
`}
`
1006
1013
`#endif // XML_GE == 1
`
1007
1014
``
`@@ -1337,7 +1344,8 @@ parserCreate(const XML_Char *encodingName,
`
1337
1344
`XML_Parser parser = NULL;
`
1338
1345
``
1339
1346
`#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);
`
1341
1349
``
1342
1350
`if (parentParser != NULL) {
`
1343
1351
`const XML_Parser rootParser = getRootParserOf(parentParser, NULL);
`
`@@ -1352,11 +1360,13 @@ parserCreate(const XML_Char *encodingName,
`
1352
1360
`if (memsuite) {
`
1353
1361
`XML_Memory_Handling_Suite *mtemp;
`
1354
1362
`#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));
`
1357
1366
`if (sizeAndParser != NULL) {
`
1358
1367
`*(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);
`
1360
1370
`#else
`
1361
1371
`parser = memsuite->malloc_fcn(sizeof(struct XML_ParserStruct));
`
1362
1372
`if (parser != NULL) {
`
`@@ -1369,11 +1379,12 @@ parserCreate(const XML_Char *encodingName,
`
1369
1379
` } else {
`
1370
1380
`XML_Memory_Handling_Suite *mtemp;
`
1371
1381
`#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));
`
1374
1384
`if (sizeAndParser != NULL) {
`
1375
1385
`*(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);
`
1377
1388
`#else
`
1378
1389
`parser = malloc(sizeof(struct XML_ParserStruct));
`
1379
1390
`if (parser != NULL) {
`
`@@ -6437,6 +6448,10 @@ internalEntityProcessor(XML_Parser parser, const char *s, const char *end,
`
6437
6448
`// process its possible inner entities (which are added to the
`
6438
6449
`// m_openInternalEntities during doProlog or doContent calls above)
`
6439
6450
`entity->hasMore = XML_FALSE;
`
``
6451
`+
if (! entity->is_param
`
``
6452
`+
&& (openEntity->startTagLevel != parser->m_tagLevel)) {
`
``
6453
`+
return XML_ERROR_ASYNC_ENTITY;
`
``
6454
`+
}
`
6440
6455
`triggerReenter(parser);
`
6441
6456
`return result;
`
6442
6457
` } // End of entity processing, "if" block will return here
`
`@@ -8135,7 +8150,7 @@ poolGrow(STRING_POOL *pool) {
`
8135
8150
`if (bytesToAllocate == 0)
`
8136
8151
`return XML_FALSE;
`
8137
8152
``
8138
``
`-
temp = REALLOC(pool->parser, pool->blocks, (unsigned)bytesToAllocate);
`
``
8153
`+
temp = REALLOC(pool->parser, pool->blocks, bytesToAllocate);
`
8139
8154
`if (temp == NULL)
`
8140
8155
`return XML_FALSE;
`
8141
8156
`pool->blocks = temp;
`