Skip to content

Commit 235e789

Browse files
committed
Make pestr[n]dup infallible
Fixes GH-9128
1 parent 04d5fae commit 235e789

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

UPGRADING.INTERNALS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ PHP 8.2 INTERNALS UPGRADE NOTES
5252
avoid duplicates when processing the same value multiple times, pass or add
5353
IS_CALLABLE_SUPPRESS_DEPRECATIONS to the check_flags parameter.
5454
* Registered zend_observer_fcall_init handlers are now also called for internal functions.
55+
* The pestrdup and pestrndup macros are now also infallible for persistent
56+
strings, so checking for NULL is no longer necessary.
5557

5658
========================
5759
2. Build system changes

Zend/zend_alloc.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3111,6 +3111,24 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
31113111
zend_out_of_memory();
31123112
}
31133113

3114+
ZEND_API char * __zend_strdup(const char *s)
3115+
{
3116+
char *tmp = strdup(s);
3117+
if (EXPECTED(tmp)) {
3118+
return tmp;
3119+
}
3120+
zend_out_of_memory();
3121+
}
3122+
3123+
ZEND_API char * ZEND_FASTCALL __zend_strndup(const char *s, size_t length)
3124+
{
3125+
char *tmp = zend_strndup(s, length);
3126+
if (EXPECTED(tmp)) {
3127+
return tmp;
3128+
}
3129+
zend_out_of_memory();
3130+
}
3131+
31143132
#ifdef ZTS
31153133
size_t zend_mm_globals_size(void)
31163134
{

Zend/zend_alloc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ typedef struct _zend_mm_debug_info {
6363
BEGIN_EXTERN_C()
6464

6565
ZEND_API ZEND_ATTRIBUTE_MALLOC char* ZEND_FASTCALL zend_strndup(const char *s, size_t length);
66+
ZEND_API ZEND_ATTRIBUTE_MALLOC char* ZEND_FASTCALL __zend_strndup(const char *s, size_t length);
6667

6768
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _emalloc(size_t size ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
6869
ZEND_API ZEND_ATTRIBUTE_MALLOC void* ZEND_FASTCALL _safe_emalloc(size_t nmemb, size_t size, size_t offset ZEND_FILE_LINE_DC ZEND_FILE_LINE_ORIG_DC);
@@ -182,6 +183,7 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
182183
ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
183184
ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
184185
ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
186+
ZEND_API ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s);
185187

186188
/* Selective persistent/non persistent allocation macros */
187189
#define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
@@ -201,8 +203,8 @@ ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2)
201203
#define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
202204
#define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
203205
#define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size)))
204-
#define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s))
205-
#define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
206+
#define pestrdup(s, persistent) ((persistent)?__zend_strdup(s):estrdup(s))
207+
#define pestrndup(s, length, persistent) ((persistent)?__zend_strndup((s),(length)):estrndup((s),(length)))
206208

207209
#define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))
208210
#define pefree_rel(ptr, persistent) ((persistent)?free(ptr):efree_rel(ptr))

sapi/cli/php_cli_server.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,21 +2490,13 @@ static zend_result php_cli_server_ctor(php_cli_server *server, const char *addr,
24902490
{
24912491
size_t document_root_len = strlen(document_root);
24922492
_document_root = pestrndup(document_root, document_root_len, 1);
2493-
if (!_document_root) {
2494-
retval = FAILURE;
2495-
goto out;
2496-
}
24972493
server->document_root = _document_root;
24982494
server->document_root_len = document_root_len;
24992495
}
25002496

25012497
if (router) {
25022498
size_t router_len = strlen(router);
25032499
_router = pestrndup(router, router_len, 1);
2504-
if (!_router) {
2505-
retval = FAILURE;
2506-
goto out;
2507-
}
25082500
server->router = _router;
25092501
server->router_len = router_len;
25102502
} else {

0 commit comments

Comments
 (0)