@@ -101,7 +101,7 @@ static int running_on_valgrind = -1;
101
101
*
102
102
* For small requests, the allocator sub-allocates <Big> blocks of memory.
103
103
* Requests greater than SMALL_REQUEST_THRESHOLD bytes are routed to the
104
- * system's allocator.
104
+ * system's allocator.
105
105
*
106
106
* Small requests are grouped in size classes spaced 8 bytes apart, due
107
107
* to the required valid alignment of the returned address. Requests of
@@ -134,7 +134,7 @@ static int running_on_valgrind = -1;
134
134
* 65-72 72 8
135
135
* ... ... ...
136
136
* 497-504 504 62
137
- * 505-512 512 63
137
+ * 505-512 512 63
138
138
*
139
139
* 0, SMALL_REQUEST_THRESHOLD + 1 and up: routed to the underlying
140
140
* allocator.
@@ -176,7 +176,7 @@ static int running_on_valgrind = -1;
176
176
* Although not required, for better performance and space efficiency,
177
177
* it is recommended that SMALL_REQUEST_THRESHOLD is set to a power of 2.
178
178
*/
179
- #define SMALL_REQUEST_THRESHOLD 512
179
+ #define SMALL_REQUEST_THRESHOLD 512
180
180
#define NB_SMALL_SIZE_CLASSES (SMALL_REQUEST_THRESHOLD / ALIGNMENT)
181
181
182
182
/*
@@ -209,7 +209,7 @@ static int running_on_valgrind = -1;
209
209
* usually an address range reservation for <Big> bytes, unless all pages within
210
210
* this space are referenced subsequently. So malloc'ing big blocks and not
211
211
* using them does not mean "wasting memory". It's an addressable range
212
- * wastage...
212
+ * wastage...
213
213
*
214
214
* Arenas are allocated with mmap() on systems supporting anonymous memory
215
215
* mappings to reduce heap fragmentation.
@@ -619,7 +619,7 @@ new_arena(void)
619
619
#else
620
620
address = malloc (ARENA_SIZE );
621
621
err = (address == 0 );
622
- #endif
622
+ #endif
623
623
if (err ) {
624
624
/* The allocation failed: return NULL after putting the
625
625
* arenaobj back.
@@ -1552,7 +1552,7 @@ _PyObject_DebugReallocApi(char api, void *p, size_t nbytes)
1552
1552
/* overflow: can't represent total as a size_t */
1553
1553
return NULL ;
1554
1554
1555
- if (nbytes < original_nbytes ) {
1555
+ if (nbytes <= original_nbytes ) {
1556
1556
/* shrinking: mark old extra memory dead */
1557
1557
memset (q + nbytes , DEADBYTE , original_nbytes - nbytes + 2 * SST );
1558
1558
}
@@ -1562,8 +1562,14 @@ _PyObject_DebugReallocApi(char api, void *p, size_t nbytes)
1562
1562
* but we live with that.
1563
1563
*/
1564
1564
q = (uchar * )PyObject_Realloc (q - 2 * SST , total );
1565
- if (q == NULL )
1565
+ if (q == NULL ) {
1566
+ if (nbytes <= original_nbytes ) {
1567
+ /* bpo-31626: the memset() above expects that realloc never fails
1568
+ on shrinking a memory block. */
1569
+ Py_FatalError ("Shrinking reallocation failed" );
1570
+ }
1566
1571
return NULL ;
1572
+ }
1567
1573
1568
1574
write_size_t (q , nbytes );
1569
1575
assert (q [SST ] == (uchar )api );
0 commit comments