diff --git a/UPGRADING.INTERNALS b/UPGRADING.INTERNALS index 47c42d48d4274..162b145fcfa1a 100644 --- a/UPGRADING.INTERNALS +++ b/UPGRADING.INTERNALS @@ -52,6 +52,8 @@ PHP 8.2 INTERNALS UPGRADE NOTES avoid duplicates when processing the same value multiple times, pass or add IS_CALLABLE_SUPPRESS_DEPRECATIONS to the check_flags parameter. * Registered zend_observer_fcall_init handlers are now also called for internal functions. +* The pestrdup and pestrndup macros and zend_strndup function are now also infallible + for persistent strings, so checking for NULL is no longer necessary. ======================== 2. Build system changes diff --git a/Zend/zend_alloc.c b/Zend/zend_alloc.c index c7895a6624c60..d0e28dca0daab 100644 --- a/Zend/zend_alloc.c +++ b/Zend/zend_alloc.c @@ -2659,6 +2659,7 @@ ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LI return p; } +static ZEND_COLD ZEND_NORETURN void zend_out_of_memory(void); ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) { @@ -2669,7 +2670,7 @@ ZEND_API char* ZEND_FASTCALL zend_strndup(const char *s, size_t length) } p = (char *) malloc(length + 1); if (UNEXPECTED(p == NULL)) { - return p; + zend_out_of_memory(); } if (EXPECTED(length)) { memcpy(p, s, length); @@ -3111,6 +3112,15 @@ ZEND_API void * __zend_realloc(void *p, size_t len) zend_out_of_memory(); } +ZEND_API char * __zend_strdup(const char *s) +{ + char *tmp = strdup(s); + if (EXPECTED(tmp)) { + return tmp; + } + zend_out_of_memory(); +} + #ifdef ZTS size_t zend_mm_globals_size(void) { diff --git a/Zend/zend_alloc.h b/Zend/zend_alloc.h index 3df0a3ba719de..578d4c78cc5c5 100644 --- a/Zend/zend_alloc.h +++ b/Zend/zend_alloc.h @@ -182,6 +182,7 @@ ZEND_API void ZEND_FASTCALL _efree_huge(void *, size_t size); ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_malloc(size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(1); ZEND_API ZEND_ATTRIBUTE_MALLOC void * __zend_calloc(size_t nmemb, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE2(1,2); ZEND_API void * __zend_realloc(void *p, size_t len) ZEND_ATTRIBUTE_ALLOC_SIZE(2); +ZEND_API ZEND_ATTRIBUTE_MALLOC char * __zend_strdup(const char *s); /* Selective persistent/non persistent allocation macros */ #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) #define safe_perealloc(ptr, nmemb, size, offset, persistent) ((persistent)?_safe_realloc((ptr), (nmemb), (size), (offset)):safe_erealloc((ptr), (nmemb), (size), (offset))) #define perealloc_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc_recoverable((ptr), (size))) #define perealloc2_recoverable(ptr, size, persistent) ((persistent)?realloc((ptr), (size)):erealloc2_recoverable((ptr), (size), (copy_size))) -#define pestrdup(s, persistent) ((persistent)?strdup(s):estrdup(s)) +#define pestrdup(s, persistent) ((persistent)?__zend_strdup(s):estrdup(s)) #define pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length))) #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size)) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index de6a7f06e2e44..541dfc464559a 100755 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -682,7 +682,7 @@ function sha1_file(string $filename, bool $binary = false): string|false {} /* syslog.c */ #ifdef HAVE_SYSLOG_H -function openlog(string $prefix, int $flags, int $facility): bool {} +function openlog(string $prefix, int $flags, int $facility): true {} function closelog(): true {} diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 2ba05ada5500d..c01805f3a3ec6 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 2615114ae250af0329b861d82f4100c2757457da */ + * Stub hash: a4c98e83e51a9546a89797b80bdd8771ef0075f9 */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_set_time_limit, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0) @@ -706,7 +706,7 @@ ZEND_END_ARG_INFO() #define arginfo_sha1_file arginfo_md5_file #if defined(HAVE_SYSLOG_H) -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openlog, 0, 3, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_openlog, 0, 3, IS_TRUE, 0) ZEND_ARG_TYPE_INFO(0, prefix, IS_STRING, 0) ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, facility, IS_LONG, 0) diff --git a/ext/standard/syslog.c b/ext/standard/syslog.c index 37ea1def88cfb..e0120a5b603b3 100644 --- a/ext/standard/syslog.c +++ b/ext/standard/syslog.c @@ -143,9 +143,6 @@ PHP_FUNCTION(openlog) free(BG(syslog_device)); } BG(syslog_device) = zend_strndup(ident, ident_len); - if(BG(syslog_device) == NULL) { - RETURN_FALSE; - } php_openlog(BG(syslog_device), option, facility); RETURN_TRUE; } diff --git a/sapi/cli/php_cli_server.c b/sapi/cli/php_cli_server.c index 7c69fdef1749e..52b1572884673 100644 --- a/sapi/cli/php_cli_server.c +++ b/sapi/cli/php_cli_server.c @@ -2490,10 +2490,6 @@ static zend_result php_cli_server_ctor(php_cli_server *server, const char *addr, { size_t document_root_len = strlen(document_root); _document_root = pestrndup(document_root, document_root_len, 1); - if (!_document_root) { - retval = FAILURE; - goto out; - } server->document_root = _document_root; server->document_root_len = document_root_len; } @@ -2501,10 +2497,6 @@ static zend_result php_cli_server_ctor(php_cli_server *server, const char *addr, if (router) { size_t router_len = strlen(router); _router = pestrndup(router, router_len, 1); - if (!_router) { - retval = FAILURE; - goto out; - } server->router = _router; server->router_len = router_len; } else {