cvs: ZendEngine2 / zend_alloc.c zend_alloc.h

From: Date: Tue, 20 Jul 2004 21:55:58 +0000
Subject: cvs: ZendEngine2 / zend_alloc.c zend_alloc.h
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-2787@lists.php.net to get a copy of this message
moriyoshi Tue Jul 20 17:55:58 2004 EDT Modified files: /ZendEngine2 zend_alloc.c zend_alloc.h Log: - Add safe_pemalloc() http://cvs.php.net/diff.php/ZendEngine2/zend_alloc.c?r1=1.138&r2=1.139&ty=u Index: ZendEngine2/zend_alloc.c diff -u ZendEngine2/zend_alloc.c:1.138 ZendEngine2/zend_alloc.c:1.139 --- ZendEngine2/zend_alloc.c:1.138 Thu Jul 15 18:59:54 2004 +++ ZendEngine2/zend_alloc.c Tue Jul 20 17:55:57 2004 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_alloc.c,v 1.138 2004/07/15 22:59:54 andi Exp $ */ +/* $Id: zend_alloc.c,v 1.139 2004/07/20 21:55:57 moriyoshi Exp $ */ #include "zend.h" #include "zend_alloc.h" @@ -241,6 +241,30 @@ 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) http://cvs.php.net/diff.php/ZendEngine2/zend_alloc.h?r1=1.54&r2=1.55&ty=u Index: ZendEngine2/zend_alloc.h diff -u ZendEngine2/zend_alloc.h:1.54 ZendEngine2/zend_alloc.h:1.55 --- ZendEngine2/zend_alloc.h:1.54 Mon Jul 19 03:19:01 2004 +++ ZendEngine2/zend_alloc.h Tue Jul 20 17:55:57 2004 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: zend_alloc.h,v 1.54 2004/07/19 07:19:01 andi Exp $ */ +/* $Id: zend_alloc.h,v 1.55 2004/07/20 21:55:57 moriyoshi Exp $ */ #ifndef ZEND_ALLOC_H #define ZEND_ALLOC_H @@ -78,6 +78,7 @@ 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 @@ /* 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)))

« previous php.zend-engine.cvs (#2787) next »