Skip to content

Make pestr[n]dup infallible #9295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion Zend/zend_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 2 additions & 1 deletion Zend/zend_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion ext/standard/basic_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down
4 changes: 2 additions & 2 deletions ext/standard/basic_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions ext/standard/syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 0 additions & 8 deletions sapi/cli/php_cli_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2490,21 +2490,13 @@ 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;
}

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 {
Expand Down