Skip to content

Commit 893bc0a

Browse files
onthebediluuu1994
andcommitted
Fix stale getopt() optional value in CLI
Technically this applies to other SAPIs, but CLI is currently the only one with a flag with an optional value. The PHP getopt() implementation already clears php_optarg by-hand. Fixes GH-21901 Closes GH-21902 Co-authored-by: Ilija Tovilo <ilija.tovilo@me.com>
1 parent c325e9b commit 893bc0a

3 files changed

Lines changed: 19 additions & 0 deletions

File tree

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.5.7
44

5+
- CLI:
6+
. Fixed bug GH-21901 (Stale getopt() optional value). (onthebed)
7+
58
- Opcache:
69
. Fixed tracing JIT crash when a VM interrupt is handled during an observed
710
user function call. (Levi Morrison)

main/getopt.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char
5959
static char **prev_optarg = NULL;
6060

6161
php_optidx = -1;
62+
if (optarg) {
63+
*optarg = NULL;
64+
}
6265

6366
if(prev_optarg && prev_optarg != optarg) {
6467
/* reset the state */

sapi/cli/tests/gh21901.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
--TEST--
2+
Stale getopt() optional value in CLI
3+
--FILE--
4+
<?php
5+
$php_escaped = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
6+
$cmd = $php_escaped . ' -n -d foo=bar --ini';
7+
echo shell_exec($cmd);
8+
?>
9+
--EXPECTF--
10+
Configuration File (php.ini) Path: "%S"
11+
Loaded Configuration File: "%S"
12+
Scan for additional .ini files in: %s
13+
Additional .ini files parsed: %s

0 commit comments

Comments
 (0)