Skip to content

Commit 98bdb7f

Browse files
authored
Make pestr[n]dup infallible (#9295)
Fixes GH-9128 Closes GH-9295
1 parent 1094a85 commit 98bdb7f

File tree

7 files changed

+18
-16
lines changed

7 files changed

+18
-16
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 and zend_strndup function are now also infallible
56+
for persistent strings, so checking for NULL is no longer necessary.
5557

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

Zend/zend_alloc.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2659,6 +2659,7 @@ ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LI
26592659
return p;
26602660
}
26612661

2662+
static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void);
26622663

26632664
ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
26642665
{
@@ -2669,7 +2670,7 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length)
26692670
}
26702671
p = (char *) malloc(length + 1);
26712672
if (UNEXPECTED(p == NULL)) {
2672-
return p;
2673+
zend_out_of_memory();
26732674
}
26742675
if (EXPECTED(length)) {
26752676
memcpy(p, s, length);
@@ -3111,6 +3112,15 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
31113112
zend_out_of_memory();
31123113
}
31133114

3115+
ZEND_API char * __zend_strdup(const char *s)
3116+
{
3117+
char *tmp = strdup(s);
3118+
if (EXPECTED(tmp)) {
3119+
return tmp;
3120+
}
3121+
zend_out_of_memory();
3122+
}
3123+
31143124
#ifdef ZTS
31153125
size_t zend_mm_globals_size(void)
31163126
{

Zend/zend_alloc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size);
182182
ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(1);
183183
ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2);
184184
ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2);
185+
ZEND_API ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s);
185186

186187
/* Selective persistent/non persistent allocation macros */
187188
#define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size))
@@ -201,7 +202,7 @@ ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2)
201202
#define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset)))
202203
#define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size)))
203204
#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 pestrdup(s, persistent) ((persistent)?__zend_strdup(s):estrdup(s))
205206
#define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
206207

207208
#define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))

ext/standard/basic_functions.stub.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ function sha1_file(string $filename, bool $binary = false): string|false {}
682682
/* syslog.c */
683683

684684
#ifdef HAVE_SYSLOG_H
685-
function openlog(string $prefix, int $flags, int $facility): bool {}
685+
function openlog(string $prefix, int $flags, int $facility): true {}
686686

687687
function closelog(): true {}
688688

ext/standard/basic_functions_arginfo.h

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/syslog.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,6 @@ PHP_FUNCTION(openlog)
143143
free(BG(syslog_device));
144144
}
145145
BG(syslog_device) = zend_strndup(ident, ident_len);
146-
if(BG(syslog_device) == NULL) {
147-
RETURN_FALSE;
148-
}
149146
php_openlog(BG(syslog_device), option, facility);
150147
RETURN_TRUE;
151148
}

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)