Skip to content

Commit c0580d7

Browse files
committed
Fix & unify zend_alloc error handling
1 parent 2c8f2e9 commit c0580d7

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

Zend/zend_alloc.c

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,17 @@ stderr_last_error(char *msg)
419419
static void *zend_mm_mmap_fixed(void *addr, size_t size)
420420
{
421421
#ifdef _WIN32
422-
return VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
422+
void *ptr = VirtualAlloc(addr, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
423+
if (ptr == NULL) {
424+
#if ZEND_MM_ERROR
425+
stderr_last_error("VirtualAlloc(addr) failed");
426+
#endif
427+
return NULL;
428+
} else if (ptr != addr) {
429+
zend_mm_munmap(ptr, size);
430+
return NULL;
431+
}
432+
return ptr;
423433
#else
424434
int flags = MAP_PRIVATE | MAP_ANON;
425435
#if defined(MAP_EXCL)
@@ -434,11 +444,7 @@ static void *zend_mm_mmap_fixed(void *addr, size_t size)
434444
#endif
435445
return NULL;
436446
} else if (ptr != addr) {
437-
if (munmap(ptr, size) != 0) {
438-
#if ZEND_MM_ERROR
439-
fprintf(stderr, "\nmunmap() failed: [%d] %s\n", errno, strerror(errno));
440-
#endif
441-
}
447+
zend_mm_munmap(ptr, size);
442448
return NULL;
443449
}
444450
return ptr;
@@ -450,10 +456,9 @@ static void *zend_mm_mmap(size_t size)
450456
{
451457
#ifdef _WIN32
452458
void *ptr = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
453-
454459
if (ptr == NULL) {
455460
#if ZEND_MM_ERROR
456-
stderr_last_error("VirtualAlloc() failed");
461+
stderr_last_error("VirtualAlloc(NULL) failed");
457462
#endif
458463
return NULL;
459464
}
@@ -1846,11 +1851,7 @@ static zend_mm_heap *zend_mm_init(void)
18461851

18471852
if (UNEXPECTED(chunk == NULL)) {
18481853
#if ZEND_MM_ERROR
1849-
#ifdef _WIN32
18501854
stderr_last_error("Can't initialize heap");
1851-
#else
1852-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
1853-
#endif
18541855
#endif
18551856
return NULL;
18561857
}
@@ -2978,11 +2979,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
29782979
chunk = (zend_mm_chunk*)handlers->chunk_alloc(&tmp_storage, ZEND_MM_CHUNK_SIZE, ZEND_MM_CHUNK_SIZE);
29792980
if (UNEXPECTED(chunk == NULL)) {
29802981
#if ZEND_MM_ERROR
2981-
#ifdef _WIN32
29822982
stderr_last_error("Can't initialize heap");
2983-
#else
2984-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
2985-
#endif
29862983
#endif
29872984
return NULL;
29882985
}
@@ -3025,11 +3022,7 @@ ZEND_API zend_mm_heap *zend_mm_startup_ex(const zend_mm_handlers *handlers, void
30253022
if (!storage) {
30263023
handlers->chunk_free(&tmp_storage, chunk, ZEND_MM_CHUNK_SIZE);
30273024
#if ZEND_MM_ERROR
3028-
#ifdef _WIN32
30293025
stderr_last_error("Can't initialize heap");
3030-
#else
3031-
fprintf(stderr, "\nCan't initialize heap: [%d] %s\n", errno, strerror(errno));
3032-
#endif
30333026
#endif
30343027
return NULL;
30353028
}

0 commit comments

Comments
 (0)