Skip to content

Commit e02c226

Browse files
committed
Merge branch 'PHP-8.4'
* PHP-8.4: ext/pcntl: Fix memory leak in cleanup code of pcntl_exec()
2 parents 1371f50 + 47b2620 commit e02c226

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

ext/pcntl/pcntl.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,9 @@ PHP_FUNCTION(pcntl_exec)
673673
envs_hash = Z_ARRVAL_P(envs);
674674
envc = zend_hash_num_elements(envs_hash);
675675

676-
pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
676+
size_t envp_len = (envc + 1);
677+
pair = envp = safe_emalloc(envp_len, sizeof(char *), 0);
678+
memset(envp, 0, sizeof(char *) * envp_len);
677679
ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
678680
if (envi >= envc) break;
679681
if (!key) {
@@ -684,9 +686,7 @@ PHP_FUNCTION(pcntl_exec)
684686

685687
if (!try_convert_to_string(element)) {
686688
zend_string_release(key);
687-
efree(argv);
688-
efree(envp);
689-
RETURN_THROWS();
689+
goto cleanup_env_vars;
690690
}
691691

692692
/* Length of element + equal sign + length of key + null */
@@ -709,6 +709,7 @@ PHP_FUNCTION(pcntl_exec)
709709
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
710710
}
711711

712+
cleanup_env_vars:
712713
/* Cleanup */
713714
for (pair = envp; *pair != NULL; pair++) efree(*pair);
714715
efree(envp);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
pcntl_exec(): Test cleanup after non-stringable array value has been encountered for $args and $env_vars.
3+
--EXTENSIONS--
4+
pcntl
5+
--FILE--
6+
<?php
7+
try {
8+
pcntl_exec('cmd', ['-n', new stdClass()]);
9+
} catch (Throwable $e) {
10+
echo $e::class, ': ', $e->getMessage(), "\n";
11+
}
12+
13+
try {
14+
pcntl_exec(
15+
'cmd',
16+
['-n'],
17+
['var1' => 'value1', 'var2' => new stdClass()],
18+
);
19+
} catch (Throwable $e) {
20+
echo $e::class, ': ', $e->getMessage(), "\n";
21+
}
22+
?>
23+
--EXPECT--
24+
Error: Object of class stdClass could not be converted to string
25+
Error: Object of class stdClass could not be converted to string

0 commit comments

Comments
 (0)