cvs: Zend(PHP_4_3) / zend_alloc.c zend_alloc.h zend_multiply.h

From: Date: Tue, 15 Apr 2003 01:30:46 +0000
Subject: cvs: Zend(PHP_4_3) / zend_alloc.c zend_alloc.h zend_multiply.h
Groups: php.zend-engine.cvs 
Request: Send a blank email to php-zend-engine-cvs+get-1139@lists.php.net to get a copy of this message
sas Mon Apr 14 21:30:46 2003 EDT Modified files: (Branch: PHP_4_3) /Zend zend_alloc.c zend_alloc.h zend_multiply.h Log: Looks like I forgot to commit the safe_emalloc function This also turns the safe multiplication function into a macro for reduced call overhead. Index: Zend/zend_alloc.c diff -u Zend/zend_alloc.c:1.105.4.2 Zend/zend_alloc.c:1.105.4.3 --- Zend/zend_alloc.c:1.105.4.2 Wed Jan 8 10:29:11 2003 +++ Zend/zend_alloc.c Mon Apr 14 21:30:46 2003 @@ -196,6 +196,32 @@ return (void *)((char *)p + sizeof(zend_mem_header) + MEM_HEADER_PADDING); } +#include "zend_multiply.h" + +ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) +{ + + 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_MAX - offset) { + return emalloc_rel(lval + offset); + } + } + + zend_error(E_ERROR, "Possible integer overflow in memory allocation (%ld * %ld + %ld)", nmemb, size, offset); + return 0; +} ZEND_API void _efree(void *ptr ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { Index: Zend/zend_alloc.h diff -u Zend/zend_alloc.h:1.39.4.1 Zend/zend_alloc.h:1.39.4.2 --- Zend/zend_alloc.h:1.39.4.1 Tue Dec 31 11:22:58 2002 +++ Zend/zend_alloc.h Mon Apr 14 21:30:46 2003 @@ -73,6 +73,7 @@ ZEND_API char *zend_strndup(const char *s, unsigned int length); ZEND_API void *_emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); +ZEND_API void *_safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); 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_API void *_erealloc(void *ptr, size_t size, int allow_failure ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC); @@ -81,6 +82,7 @@ /* Standard wrapper macros */ #define emalloc(size) _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) +#define safe_emalloc(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define efree(ptr) _efree((ptr) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define ecalloc(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) #define erealloc(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_EMPTY_CC) @@ -90,6 +92,7 @@ /* Relay wrapper macros */ #define emalloc_rel(size) _emalloc((size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC) +#define safe_emalloc_rel(nmemb, size, offset) _safe_emalloc((nmemb), (size), (offset) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC) #define efree_rel(ptr) _efree((ptr) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC) #define ecalloc_rel(nmemb, size) _ecalloc((nmemb), (size) ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC) #define erealloc_rel(ptr, size) _erealloc((ptr), (size), 0 ZEND_FILE_LINE_RELAY_CC ZEND_FILE_LINE_CC) Index: Zend/zend_multiply.h diff -u Zend/zend_multiply.h:1.1.2.1 Zend/zend_multiply.h:1.1.2.2 --- Zend/zend_multiply.h:1.1.2.1 Thu Apr 3 04:41:45 2003 +++ Zend/zend_multiply.h Mon Apr 14 21:30:46 2003 @@ -19,67 +19,27 @@ /* for zend_bool and limits.h */ #include "zend.h" -typedef enum { - S_MP_OK, - S_MP_OVERFLOW, - S_MP_OVERFLOW_FP_STORED -} zend_mul_result; - -static inline zend_mul_result zend_signed_multiply_long(long a, long b, - long *result, double *dresult) - #if defined(__i386__) && defined(__GNUC__) - -{ - int of; - - __asm__ ("imul %3,%0\n" \ - "adc $0,%1" - : "=r"(a),"=r"(of) - : "0"(a), "r"(b), "1"(0)); - - if (of) { - return S_MP_OVERFLOW; - } - *result = a; - - return S_MP_OK; -} +#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \ + __asm__ ("imul %3,%0\n" \ + "adc $0,%1" \ + : "=r"(a),"=r"(usedval) \ + : "0"(a), "r"(b), "1"(0)); \ + if (usedval) (dval) = (double) (a) * (double) (b); \ +} while (0) #else -{ - double dr; - - dr = (double) a * (double) b; - if (dr >= LONG_MAX || dr <= LONG_MIN) { - if (dresult) { - *dresult = dr; - return S_MP_OVERFLOW_FP_STORED; - } - return S_MP_OVERFLOW; - } - - *result = a * b; - return S_MP_OK; -} +#define ZEND_SIGNED_MULTIPLY_LONG(a, b, lval, dval, usedval) do { \ + (dval) = (double) (a) * (double) (b); \ + \ + if ((dval) >= LONG_MAX || (dval) <= LONG_MIN) { \ + (usedval) = 1; \ + } else { \ + (lval) = (a) * (b); \ + (usedval) = 0; \ + } \ +} while (0) #endif - -/* always provides dresult in case of overflow */ -static inline zend_bool zend_signed_multiply_long_ex(long a, long b, - long *result, double *dresult) -{ - assert(result != NULL); - assert(dresult != NULL); - - switch (zend_signed_multiply_long(a, b, result, dresult)) { - case S_MP_OVERFLOW: - *dresult = (double) a * (double) b; - /* fall-thru */ - case S_MP_OVERFLOW_FP_STORED: - return FAILURE; - } - return SUCCESS; -}

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