Make pestr[n]dup infallible (#9295)

Fixes GH-9128
Closes GH-9295
This commit is contained in:
Ilija Tovilo 2022-08-12 12:21:14 +02:00 committed by GitHub
parent 1094a859ad
commit 98bdb7f99b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 16 deletions

View File

@ -52,6 +52,8 @@ PHP 8.2 INTERNALS UPGRADE NOTES
avoid duplicates when processing the same value multiple times, pass or add avoid duplicates when processing the same value multiple times, pass or add
IS_CALLABLE_SUPPRESS_DEPRECATIONS to the check_flags parameter. IS_CALLABLE_SUPPRESS_DEPRECATIONS to the check_flags parameter.
* Registered zend_observer_fcall_init handlers are now also called for internal functions. * 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 2. Build system changes

View File

@ -2659,6 +2659,7 @@ ZEND_API char* ZEND_FASTCALL _estrndup(const char *s, size_t length ZEND_FILE_LI
return p; 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) 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); p = (char *) malloc(length + 1);
if (UNEXPECTED(p == NULL)) { if (UNEXPECTED(p == NULL)) {
return p; zend_out_of_memory();
} }
if (EXPECTED(length)) { if (EXPECTED(length)) {
memcpy(p, s, length); memcpy(p, s, length);
@ -3111,6 +3112,15 @@ ZEND_API void * __zend_realloc(void *p, size_t len)
zend_out_of_memory(); 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 #ifdef ZTS
size_t zend_mm_globals_size(void) size_t zend_mm_globals_size(void)
{ {

View File

@ -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_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 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 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 */ /* Selective persistent/non persistent allocation macros */
#define pemalloc(size, persistent) ((persistent)?__zend_malloc(size):emalloc(size)) #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 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 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 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 pestrndup(s, length, persistent) ((persistent)?zend_strndup((s),(length)):estrndup((s),(length)))
#define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size)) #define pemalloc_rel(size, persistent) ((persistent)?__zend_malloc(size):emalloc_rel(size))

View File

@ -682,7 +682,7 @@ function sha1_file(string $filename, bool $binary = false): string|false {}
/* syslog.c */ /* syslog.c */
#ifdef HAVE_SYSLOG_H #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 {} function closelog(): true {}

View File

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead. /* 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_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) ZEND_ARG_TYPE_INFO(0, seconds, IS_LONG, 0)
@ -706,7 +706,7 @@ ZEND_END_ARG_INFO()
#define arginfo_sha1_file arginfo_md5_file #define arginfo_sha1_file arginfo_md5_file
#if defined(HAVE_SYSLOG_H) #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, prefix, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, facility, IS_LONG, 0) ZEND_ARG_TYPE_INFO(0, facility, IS_LONG, 0)

View File

@ -143,9 +143,6 @@ PHP_FUNCTION(openlog)
free(BG(syslog_device)); free(BG(syslog_device));
} }
BG(syslog_device) = zend_strndup(ident, ident_len); BG(syslog_device) = zend_strndup(ident, ident_len);
if(BG(syslog_device) == NULL) {
RETURN_FALSE;
}
php_openlog(BG(syslog_device), option, facility); php_openlog(BG(syslog_device), option, facility);
RETURN_TRUE; RETURN_TRUE;
} }

View File

@ -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); size_t document_root_len = strlen(document_root);
_document_root = pestrndup(document_root, document_root_len, 1); _document_root = pestrndup(document_root, document_root_len, 1);
if (!_document_root) {
retval = FAILURE;
goto out;
}
server->document_root = _document_root; server->document_root = _document_root;
server->document_root_len = document_root_len; 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) { if (router) {
size_t router_len = strlen(router); size_t router_len = strlen(router);
_router = pestrndup(router, router_len, 1); _router = pestrndup(router, router_len, 1);
if (!_router) {
retval = FAILURE;
goto out;
}
server->router = _router; server->router = _router;
server->router_len = router_len; server->router_len = router_len;
} else { } else {