mirror of
https://github.com/php/php-src.git
synced 2025-01-26 13:44:22 +08:00
- Add safe_pemalloc()
This commit is contained in:
parent
23d202bd36
commit
182e93ab6e
@ -241,6 +241,30 @@ ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_
|
||||
return 0;
|
||||
}
|
||||
|
||||
ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset)
|
||||
{
|
||||
|
||||
if (nmemb < LONG_MAX
|
||||
&& size < LONG_MAX
|
||||
&& offset < LONG_MAX
|
||||
&& nmemb >= 0
|
||||
&& size >= 0
|
||||
&& offset >= 0) {
|
||||
long lval;
|
||||
double dval;
|
||||
int use_dval;
|
||||
|
||||
ZEND_SIGNED_MULTIPLY_LONG(nmemb, size, lval, dval, use_dval);
|
||||
|
||||
if (!use_dval
|
||||
&& lval < (long) (LONG_MAX - offset)) {
|
||||
return pemalloc(lval + offset, 1);
|
||||
}
|
||||
}
|
||||
|
||||
zend_error(E_ERROR, "Possible integer overflow in memory allocation (%zd * %zd + %zd)", nmemb, size, offset);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
|
||||
|
@ -78,6 +78,7 @@ ZEND_API char *zend_strndup(const char *s, unsigned int length) ZEND_ATTRIBUTE_M
|
||||
|
||||
ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
||||
ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
||||
ZEND_API void *_safe_malloc(size_t nmemb, size_t size, size_t offset) ZEND_ATTRIBUTE_MALLOC;
|
||||
ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
||||
ZEND_API void *_ecalloc(size_t nmemb, size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_MALLOC;
|
||||
ZEND_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
|
||||
@ -106,6 +107,7 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE
|
||||
|
||||
/* Selective persistent/non persistent allocation macros */
|
||||
#define pemalloc(size, persistent) ((persistent)?malloc(size):emalloc(size))
|
||||
#define safe_pemalloc(nmemb, size, offset, persistent) ((persistent)?_safe_malloc(nmemb, size, offset):safe_emalloc(nmemb, size, offset))
|
||||
#define pefree(ptr, persistent) ((persistent)?free(ptr):efree(ptr))
|
||||
#define pecalloc(nmemb, size, persistent) ((persistent)?calloc((nmemb), (size)):ecalloc((nmemb), (size)))
|
||||
#define perealloc(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc((ptr), (size)))
|
||||
|
Loading…
Reference in New Issue
Block a user