Make calloc set ENOMEM when failing due to overflow.

This fixes a bug in upstream dlmalloc, where it doesn't set errno to
ENOMEM in overflow or footprint overrun cases.
This commit is contained in:
Dan Gohman 2019-04-22 11:36:54 -07:00
parent 972067b74b
commit 0df6243d52

View File

@ -4055,12 +4055,26 @@ static void* sys_alloc(mstate m, size_t nb) {
} }
asize = granularity_align(nb + SYS_ALLOC_PADDING); asize = granularity_align(nb + SYS_ALLOC_PADDING);
#ifdef __wasilibc_unmodified_upstream // Bug fix: set ENOMEM on size overflow
if (asize <= nb) if (asize <= nb)
return 0; /* wraparound */ return 0; /* wraparound */
#else
if (asize <= nb) {
MALLOC_FAILURE_ACTION;
return 0; /* wraparound */
}
#endif
if (m->footprint_limit != 0) { if (m->footprint_limit != 0) {
size_t fp = m->footprint + asize; size_t fp = m->footprint + asize;
#ifdef __wasilibc_unmodified_upstream // Bug fix: set ENOMEM on footprint overrun
if (fp <= m->footprint || fp > m->footprint_limit) if (fp <= m->footprint || fp > m->footprint_limit)
return 0; return 0;
#else
if (fp <= m->footprint || fp > m->footprint_limit) {
MALLOC_FAILURE_ACTION;
return 0;
}
#endif
} }
/* /*