Skip to content

Commit 2df9f32

Browse files
committed
ext/pcntl: Fix memory leak in cleanup code of pcntl_exec()
1 parent ee0daa5 commit 2df9f32

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ PHP NEWS
2323
- Opcache:
2424
. opcache_get_configuration() properly reports jit_prof_threshold. (cmb)
2525

26+
- PCNTL:
27+
. Fix memory leak in cleanup code of pcntl_exec() when a non stringable
28+
value is encountered past the first entry. (Girgias)
29+
2630
- PgSql:
2731
. Fixed bug GH-17158 (pg_fetch_result Shows Incorrect ArgumentCountError
2832
Message when Called With 1 Argument). (nielsdos)

ext/pcntl/pcntl.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,9 @@ PHP_FUNCTION(pcntl_exec)
540540
envs_hash = Z_ARRVAL_P(envs);
541541
envc = zend_hash_num_elements(envs_hash);
542542

543-
pair = envp = safe_emalloc((envc + 1), sizeof(char *), 0);
543+
size_t envp_len = (envc + 1);
544+
pair = envp = safe_emalloc(envp_len, sizeof(char *), 0);
545+
memset(envp, 0, sizeof(char *) * envp_len);
544546
ZEND_HASH_FOREACH_KEY_VAL(envs_hash, key_num, key, element) {
545547
if (envi >= envc) break;
546548
if (!key) {
@@ -551,9 +553,7 @@ PHP_FUNCTION(pcntl_exec)
551553

552554
if (!try_convert_to_string(element)) {
553555
zend_string_release(key);
554-
efree(argv);
555-
efree(envp);
556-
RETURN_THROWS();
556+
goto cleanup_env_vars;
557557
}
558558

559559
/* Length of element + equal sign + length of key + null */
@@ -576,6 +576,7 @@ PHP_FUNCTION(pcntl_exec)
576576
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
577577
}
578578

579+
cleanup_env_vars:
579580
/* Cleanup */
580581
for (pair = envp; *pair != NULL; pair++) efree(*pair);
581582
efree(envp);
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)