Commit 893bc0ad6e5 for php.net
commit 893bc0ad6e5a781d8b47fa42fb9f21623ea404b6
Author: onthebed <1136664562@qq.com>
Date: Wed Apr 29 03:23:15 2026 +0800
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>
diff --git a/NEWS b/NEWS
index 3553ae75517..326245a6956 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,9 @@ PHP NEWS
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
?? ??? ????, PHP 8.5.7
+- CLI:
+ . Fixed bug GH-21901 (Stale getopt() optional value). (onthebed)
+
- Opcache:
. Fixed tracing JIT crash when a VM interrupt is handled during an observed
user function call. (Levi Morrison)
diff --git a/main/getopt.c b/main/getopt.c
index 65ce94ae9bb..1d952555de5 100644
--- a/main/getopt.c
+++ b/main/getopt.c
@@ -59,6 +59,9 @@ PHPAPI int php_getopt(int argc, char* const *argv, const opt_struct opts[], char
static char **prev_optarg = NULL;
php_optidx = -1;
+ if (optarg) {
+ *optarg = NULL;
+ }
if(prev_optarg && prev_optarg != optarg) {
/* reset the state */
diff --git a/sapi/cli/tests/gh21901.phpt b/sapi/cli/tests/gh21901.phpt
new file mode 100644
index 00000000000..d469df60137
--- /dev/null
+++ b/sapi/cli/tests/gh21901.phpt
@@ -0,0 +1,13 @@
+--TEST--
+Stale getopt() optional value in CLI
+--FILE--
+<?php
+$php_escaped = getenv('TEST_PHP_EXECUTABLE_ESCAPED');
+$cmd = $php_escaped . ' -n -d foo=bar --ini';
+echo shell_exec($cmd);
+?>
+--EXPECTF--
+Configuration File (php.ini) Path: "%S"
+Loaded Configuration File: "%S"
+Scan for additional .ini files in: %s
+Additional .ini files parsed: %s