From 779f5c5c195037347699d7f352d16063adb80a8c Mon Sep 17 00:00:00 2001 From: Zeev Suraski Date: Thu, 7 Apr 2005 13:59:45 +0000 Subject: [PATCH] Fix strdup() bug when USE_ZEND_ALLOC is 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 78a0a18542405..79041ed263c97 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -409,6 +409,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 1a85e74dc44eb..c35661996c819 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -134,6 +134,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)) @@ -142,7 +144,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) @@ -152,7 +154,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) @@ -171,7 +173,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