Add compat for reallocarray()
This function fails safely in the case where the multiplication would overflow.
This commit is contained in:
parent
6e05d7047a
commit
6dceb32817
3 changed files with 19 additions and 1 deletions
|
@ -169,6 +169,7 @@ check_functions = [
|
||||||
'vasprintf',
|
'vasprintf',
|
||||||
'nrand48',
|
'nrand48',
|
||||||
'jrand48',
|
'jrand48',
|
||||||
|
'reallocarray',
|
||||||
]
|
]
|
||||||
|
|
||||||
foreach f : check_functions
|
foreach f : check_functions
|
||||||
|
|
|
@ -3,6 +3,9 @@
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#ifndef HAVE_REALLOCARRAY
|
||||||
|
# include <errno.h>
|
||||||
|
#endif
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
|
@ -93,5 +96,15 @@ long jrand48(unsigned short xsubi[3]) {
|
||||||
return v.i;
|
return v.i;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_REALLOCARRAY
|
||||||
|
void *reallocarray(void *ptr, size_t nmemb, size_t size) {
|
||||||
|
size_t bytes;
|
||||||
|
if (__builtin_mul_overflow(nmemb, size, &bytes)) {
|
||||||
|
errno = ENOMEM;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return realloc(ptr, bytes);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -67,4 +67,8 @@ long nrand48(unsigned short xsubi[3]);
|
||||||
long jrand48(unsigned short xsubi[3]);
|
long jrand48(unsigned short xsubi[3]);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef HAVE_REALLOCARRAY
|
||||||
|
void *reallocarray(void *ptr, size_t nmemb, size_t size);
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue