From 1bb1e8f5fa72a5c35afc8726a52552e890542055 Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Thu, 7 Apr 2005 14:00:00 +0000 Subject: [PATCH] MFH (Fix strdup() bug when USE_ZEND_ALLOC was disabled) --- Zend/zend_alloc.c | 9 +++++++++ Zend/zend_alloc.h | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index e43ec4e532..5e3c70d25c 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -388,6 +388,15 @@ ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) return p; } +#if !USE_ZEND_ALLOC +char *_strndup(char *s, uint l) +{ + char *tmp = malloc(l+1); + tmp[l] = '\0'; + memcpy(tmp, s, l); + return tmp; +} +#endif ZEND_API char *_estrndup(const char *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) { diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index 856919188f..62adc8e662 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -133,6 +133,8 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE #include #undef _GNU_SOURCE +char *_strndup(char *s, uint l); + /* Standard wrapper macros */ #define emalloc(size) malloc(size) #define safe_emalloc(nmemb, size, offset) malloc((nmemb) * (size) + (offset)) @@ -141,7 +143,7 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE #define erealloc(ptr, size) realloc((ptr), (size)) #define erealloc_recoverable(ptr, size) realloc((ptr), (size)) #define estrdup(s) strdup(s) -#define estrndup(s, length) strndup((s), (length)) +#define estrndup(s, length) _strndup((s), (length)) /* Relay wrapper macros */ #define emalloc_rel(size) malloc(size) @@ -151,7 +153,7 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE #define erealloc_rel(ptr, size) realloc((ptr), (size)) #define erealloc_recoverable_rel(ptr, size) realloc((ptr), (size)) #define estrdup_rel(s) strdup(s) -#define estrndup_rel(s, length) strndup((s), (length)) +#define estrndup_rel(s, length) _strndup((s), (length)) /* Selective persistent/non persistent allocation macros */ #define pemalloc(size, persistent) malloc(size) @@ -169,7 +171,7 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE #define pestrdup_rel(s, persistent) strdup(s) #define safe_estrdup(ptr) ((ptr)?(strdup(ptr)):(empty_string)) -#define safe_estrndup(ptr, len) ((ptr)?(strndup((ptr), (len))):(empty_string)) +#define safe_estrndup(ptr, len) ((ptr)?(_strndup((ptr), (len))):(empty_string)) #endif -- 2.50.1