* libbb/xfuncs.c (xmalloc, xcalloc): Do not exit if a zero-length buffer is
requested. (xrealloc): Simplify.
This commit is contained in:
parent
eed9451cf6
commit
a99b194336
@ -30,26 +30,15 @@
|
|||||||
extern void *xmalloc(size_t size)
|
extern void *xmalloc(size_t size)
|
||||||
{
|
{
|
||||||
void *ptr = malloc(size);
|
void *ptr = malloc(size);
|
||||||
|
if (ptr == NULL && size != 0)
|
||||||
if (!ptr)
|
|
||||||
error_msg_and_die(memory_exhausted);
|
error_msg_and_die(memory_exhausted);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void *xrealloc(void *old, size_t size)
|
extern void *xrealloc(void *ptr, size_t size)
|
||||||
{
|
{
|
||||||
void *ptr;
|
ptr = realloc(ptr, size);
|
||||||
|
if (ptr == NULL && size != 0)
|
||||||
/* SuS2 says "If size is 0 and ptr is not a null pointer, the
|
|
||||||
* object pointed to is freed." Do that here, in case realloc
|
|
||||||
* returns a NULL, since we don't want to choke in that case. */
|
|
||||||
if (size==0 && old) {
|
|
||||||
free(old);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ptr = realloc(old, size);
|
|
||||||
if (!ptr)
|
|
||||||
error_msg_and_die(memory_exhausted);
|
error_msg_and_die(memory_exhausted);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
@ -57,7 +46,7 @@ extern void *xrealloc(void *old, size_t size)
|
|||||||
extern void *xcalloc(size_t nmemb, size_t size)
|
extern void *xcalloc(size_t nmemb, size_t size)
|
||||||
{
|
{
|
||||||
void *ptr = calloc(nmemb, size);
|
void *ptr = calloc(nmemb, size);
|
||||||
if (!ptr)
|
if (ptr == NULL && nmemb != 0 && size != 0)
|
||||||
error_msg_and_die(memory_exhausted);
|
error_msg_and_die(memory_exhausted);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user