Skip to content

Commit 47b2620

Browse files
committed
Merge branch 'PHP-8.3' into PHP-8.4
* PHP-8.3: ext/pcntl: Fix memory leak in cleanup code of pcntl_exec()
2 parents 08b14a5 + 2df9f32 commit 47b2620

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ PHP NEWS
3838
- Opcache:
3939
. opcache_get_configuration() properly reports jit_prof_threshold. (cmb)
4040

41+
- PCNTL:
42+
. Fix memory leak in cleanup code of pcntl_exec() when a non stringable
43+
value is encountered past the first entry. (Girgias)
44+
4145
- PgSql:
4246
. Fixed bug GH-17158 (pg_fetch_result Shows Incorrect ArgumentCountError
4347
Message when Called With 1 Argument). (nielsdos)

ext/pcntl/pcntl.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,9 @@ PHP_FUNCTION(pcntl_exec)
678678
envs_hash = Z_ARRVAL_P(envs);
679679
envc = zend_hash_num_elements(envs_hash);
680680

681-
pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
681+
size_t envp_len = (envc + 1);
682+
pair = envp = safe_emalloc(envp_len, sizeof(char *), 0);
683+
memset(envp, 0, sizeof(char *) * envp_len);
682684
ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
683685
if (envi >= envc) break;
684686
if (!key) {
@@ -689,9 +691,7 @@ PHP_FUNCTION(pcntl_exec)
689691

690692
if (!try_convert_to_string(element)) {
691693
zend_string_release(key);
692-
efree(argv);
693-
efree(envp);
694-
RETURN_THROWS();
694+
goto cleanup_env_vars;
695695
}
696696

697697
/* Length of element + equal sign + length of key + null */
@@ -714,6 +714,7 @@ PHP_FUNCTION(pcntl_exec)
714714
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
715715
}
716716

717+
cleanup_env_vars:
717718
/* Cleanup */
718719
for (pair = envp; *pair != NULL; pair++) efree(*pair);
719720
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)