Skip to content

Commit 30e5aff

Browse files
authored
bpo-37732: Fix GCC warning in _PyObject_Malloc() (GH-15333) (GH-15342)
pymalloc_alloc() now returns directly the pointer, return NULL on memory allocation error. allocate_from_new_pool() already uses NULL as marker for "allocation failed". (cherry picked from commit 18f8dcf)
1 parent 1271ee8 commit 30e5aff

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

Objects/obmalloc.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,13 +1415,13 @@ address_in_range(void *p, poolp pool)
14151415
block allocations typically result in a couple of instructions).
14161416
Unless the optimizer reorders everything, being too smart...
14171417
1418-
Return 1 if pymalloc allocated memory and wrote the pointer into *ptr_p.
1418+
Return a pointer to newly allocated memory if pymalloc allocated memory.
14191419
1420-
Return 0 if pymalloc failed to allocate the memory block: on bigger
1420+
Return NULL if pymalloc failed to allocate the memory block: on bigger
14211421
requests, on error in the code below (as a last chance to serve the request)
14221422
or when the max memory limit has been reached. */
1423-
static int
1424-
pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
1423+
static void*
1424+
pymalloc_alloc(void *ctx, size_t nbytes)
14251425
{
14261426
block *bp;
14271427
poolp pool;
@@ -1433,15 +1433,15 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
14331433
running_on_valgrind = RUNNING_ON_VALGRIND;
14341434
}
14351435
if (UNLIKELY(running_on_valgrind)) {
1436-
return 0;
1436+
return NULL;
14371437
}
14381438
#endif
14391439

14401440
if (nbytes == 0) {
1441-
return 0;
1441+
return NULL;
14421442
}
14431443
if (nbytes > SMALL_REQUEST_THRESHOLD) {
1444-
return 0;
1444+
return NULL;
14451445
}
14461446

14471447
/*
@@ -1609,19 +1609,18 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
16091609

16101610
success:
16111611
assert(bp != NULL);
1612-
*ptr_p = (void *)bp;
1613-
return 1;
1612+
return (void *)bp;
16141613

16151614
failed:
1616-
return 0;
1615+
return NULL;
16171616
}
16181617

16191618

16201619
static void *
16211620
_PyObject_Malloc(void *ctx, size_t nbytes)
16221621
{
1623-
void* ptr;
1624-
if (pymalloc_alloc(ctx, &ptr, nbytes)) {
1622+
void* ptr = pymalloc_alloc(ctx, nbytes);
1623+
if (ptr != NULL) {
16251624
_Py_AllocatedBlocks++;
16261625
return ptr;
16271626
}
@@ -1637,12 +1636,11 @@ _PyObject_Malloc(void *ctx, size_t nbytes)
16371636
static void *
16381637
_PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
16391638
{
1640-
void* ptr;
1641-
16421639
assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
16431640
size_t nbytes = nelem * elsize;
16441641

1645-
if (pymalloc_alloc(ctx, &ptr, nbytes)) {
1642+
void *ptr = pymalloc_alloc(ctx, nbytes);
1643+
if (ptr != NULL) {
16461644
memset(ptr, 0, nbytes);
16471645
_Py_AllocatedBlocks++;
16481646
return ptr;
@@ -1743,8 +1741,8 @@ pymalloc_free(void *ctx, void *p)
17431741
* are no arenas in usable_arenas with that value.
17441742
*/
17451743
struct arena_object* lastnf = nfp2lasta[nf];
1746-
assert((nf == 0 && lastnf == NULL) ||
1747-
(nf > 0 &&
1744+
assert((nf == 0 && lastnf == NULL) ||
1745+
(nf > 0 &&
17481746
lastnf != NULL &&
17491747
lastnf->nfreepools == nf &&
17501748
(lastnf->nextarena == NULL ||

0 commit comments

Comments
 (0)