Skip to content

Commit 779f5c5

Browse files
committed
Fix strdup() bug when USE_ZEND_ALLOC is disabled
1 parent cd47b3c commit 779f5c5

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Zend/zend_alloc.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,15 @@ ZEND_API char *_estrdup(const char *s ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
409409
return p;
410410
}
411411

412+
#if !USE_ZEND_ALLOC
413+
char *_strndup(char *s, uint l)
414+
{
415+
char *tmp = malloc(l+1);
416+
tmp[l] = '\0';
417+
memcpy(tmp, s, l);
418+
return tmp;
419+
}
420+
#endif
412421

413422
ZEND_API char *_estrndup(const char *s, uint length ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC)
414423
{

Zend/zend_alloc.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ ZEND_API char *_estrndup(const char *s, unsigned int length ZEND_FILE_LINE_DC ZE
134134
#include <string.h>
135135
#undef _GNU_SOURCE
136136

137+
char *_strndup(char *s, uint l);
138+
137139
/* Standard wrapper macros */
138140
#define emalloc(size) malloc(size)
139141
#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
142144
#define erealloc(ptr, size) realloc((ptr), (size))
143145
#define erealloc_recoverable(ptr, size) realloc((ptr), (size))
144146
#define estrdup(s) strdup(s)
145-
#define estrndup(s, length) strndup((s), (length))
147+
#define estrndup(s, length) _strndup((s), (length))
146148

147149
/* Relay wrapper macros */
148150
#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
152154
#define erealloc_rel(ptr, size) realloc((ptr), (size))
153155
#define erealloc_recoverable_rel(ptr, size) realloc((ptr), (size))
154156
#define estrdup_rel(s) strdup(s)
155-
#define estrndup_rel(s, length) strndup((s), (length))
157+
#define estrndup_rel(s, length) _strndup((s), (length))
156158

157159
/* Selective persistent/non persistent allocation macros */
158160
#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
171173
#define pestrdup_rel(s, persistent) strdup(s)
172174

173175
#define safe_estrdup(ptr) ((ptr)?(strdup(ptr)):(empty_string))
174-
#define safe_estrndup(ptr, len) ((ptr)?(strndup((ptr), (len))):(empty_string))
176+
#define safe_estrndup(ptr, len) ((ptr)?(_strndup((ptr), (len))):(empty_string))
175177

176178
#endif
177179

0 commit comments

Comments
 (0)