Skip to content

Commit c9a484a

Browse files
authored
bpo-37732: Fix GCC warning in _PyObject_Malloc() (GH-15333) (GH-15342) (GH-15343)
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) (cherry picked from commit 30e5aff)
1 parent 02c1457 commit c9a484a

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

Objects/obmalloc.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,13 +1377,13 @@ address_in_range(void *p, poolp pool)
13771377
block allocations typically result in a couple of instructions).
13781378
Unless the optimizer reorders everything, being too smart...
13791379
1380-
Return 1 if pymalloc allocated memory and wrote the pointer into *ptr_p.
1380+
Return a pointer to newly allocated memory if pymalloc allocated memory.
13811381
1382-
Return 0 if pymalloc failed to allocate the memory block: on bigger
1382+
Return NULL if pymalloc failed to allocate the memory block: on bigger
13831383
requests, on error in the code below (as a last chance to serve the request)
13841384
or when the max memory limit has been reached. */
1385-
static int
1386-
pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
1385+
static void*
1386+
pymalloc_alloc(void *ctx, size_t nbytes)
13871387
{
13881388
block *bp;
13891389
poolp pool;
@@ -1395,15 +1395,15 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
13951395
running_on_valgrind = RUNNING_ON_VALGRIND;
13961396
}
13971397
if (UNLIKELY(running_on_valgrind)) {
1398-
return 0;
1398+
return NULL;
13991399
}
14001400
#endif
14011401

14021402
if (nbytes == 0) {
1403-
return 0;
1403+
return NULL;
14041404
}
14051405
if (nbytes > SMALL_REQUEST_THRESHOLD) {
1406-
return 0;
1406+
return NULL;
14071407
}
14081408

14091409
LOCK();
@@ -1564,20 +1564,19 @@ pymalloc_alloc(void *ctx, void **ptr_p, size_t nbytes)
15641564
success:
15651565
UNLOCK();
15661566
assert(bp != NULL);
1567-
*ptr_p = (void *)bp;
1568-
return 1;
1567+
return (void *)bp;
15691568

15701569
failed:
15711570
UNLOCK();
1572-
return 0;
1571+
return NULL;
15731572
}
15741573

15751574

15761575
static void *
15771576
_PyObject_Malloc(void *ctx, size_t nbytes)
15781577
{
1579-
void* ptr;
1580-
if (pymalloc_alloc(ctx, &ptr, nbytes)) {
1578+
void* ptr = pymalloc_alloc(ctx, nbytes);
1579+
if (ptr != NULL) {
15811580
_Py_AllocatedBlocks++;
15821581
return ptr;
15831582
}
@@ -1593,12 +1592,11 @@ _PyObject_Malloc(void *ctx, size_t nbytes)
15931592
static void *
15941593
_PyObject_Calloc(void *ctx, size_t nelem, size_t elsize)
15951594
{
1596-
void* ptr;
1597-
15981595
assert(elsize == 0 || nelem <= (size_t)PY_SSIZE_T_MAX / elsize);
15991596
size_t nbytes = nelem * elsize;
16001597

1601-
if (pymalloc_alloc(ctx, &ptr, nbytes)) {
1598+
void *ptr = pymalloc_alloc(ctx, nbytes);
1599+
if (ptr != NULL) {
16021600
memset(ptr, 0, nbytes);
16031601
_Py_AllocatedBlocks++;
16041602
return ptr;

0 commit comments

Comments
 (0)