Skip to content

Commit

Permalink
Fix strdup() bug when USE_ZEND_ALLOC is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zsuraski committed Apr 7, 2005
1 parent cd47b3c commit 779f5c5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
9 changes: 9 additions & 0 deletions Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 5 additions & 3 deletions Zend/zend_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE
#include <string.h>
#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))
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit 779f5c5

Please sign in to comment.