Page MenuHomePhabricator

PHP 5.5.9's getopt does not respect global $argv like HHVM does, causing issues parsing command line parameters via multiversion's MWScript.
Closed, ResolvedPublic

Description

I was tracking down an issue that now appears to be separate in importImages (https://gerrit.wikimedia.org/r/#/c/268314/) while I stumbled into this weirdness through mcc.php. Wikimedia servers have been affected since https://gerrit.wikimedia.org/r/#/c/267816/

1krenair@terbium:~$ sudo -u www-data php "/srv/mediawiki/multiversion/MWScript.php" testP2557.php commonswiki --help
2array(1) {
3 ["help"]=>
4 bool(false)
5}
6array(3) {
7 [0]=>
8 string(13) "testP2557.php"
9 [1]=>
10 string(18) "--wiki=commonswiki"
11 [2]=>
12 string(6) "--help"
13}
14krenair@terbium:~$ sudo -u www-data php5 "/srv/mediawiki/multiversion/MWScript.php" testP2557.php commonswiki --help
15array(0) {
16}
17array(3) {
18 [0]=>
19 string(13) "testP2557.php"
20 [1]=>
21 string(18) "--wiki=commonswiki"
22 [2]=>
23 string(6) "--help"
24}
25krenair@terbium:~$ php5 --version
26PHP 5.5.9-1ubuntu4.14 (cli) (built: Oct 28 2015 01:34:46)
27Copyright (c) 1997-2014 The PHP Group
28Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
29 with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
30krenair@terbium:~$ php --version
31HipHop VM 3.6.5 (rel)
32Compiler: 1443697402_580739891
33Repo schema: 10f852e58e0252284afc3158c4951c7db69aecd2
34Extension API: 20150212
35krenair@terbium:~$ cat /srv/mediawiki/php-1.27.0-wmf.10/maintenance/testP2557.php
36<?php
37//require_once __DIR__ . '/commandLine.inc';
38$options = getopt( '', array( 'debug', 'help', 'cache:' ) );
39var_dump( $options, $argv );

Event Timeline

Krenair created this task.Feb 3 2016, 11:35 PM
Krenair raised the priority of this task from to Needs Triage.
Krenair updated the task description. (Show Details)
Restricted Application added subscribers: StudiesWorld, Aklapper. · View Herald TranscriptFeb 3 2016, 11:35 PM
Krenair removed a project: [DO NOT USE] NewPHP.
Krenair set Security to None.

Docs for getopt say:

The parsing of options will end at the first non-option found, anything that follows is discarded.

So looks like PHP thinks testP2557.php is the first option. MWScript.php messes with $argv but I don't think PHP takes the data from this variable. From the code it looks like it uses $_SERVER['argv']. Also, looks like directly modifying it won't help much - it still uses the original value.

Krenair renamed this task from PHP 5.5.9 seems to have issues parsing some argumentless command line parameters to PHP 5.5.9 seems to have issues parsing some command line parameters via multiversion's MWScript.Feb 4 2016, 4:46 PM
krenair@terbium:~$ cat /srv/mediawiki/php-1.27.0-wmf.12/maintenance/testT125748.php
<?php
$options = getopt( '', array( 'help' ) );
var_dump( $options, $argv, $_SERVER["argv"] );
krenair@terbium:~$ sudo -u www-data php "/srv/mediawiki/multiversion/MWScript.php" testT125748.php commonswiki --help
array(1) {
  ["help"]=>
  bool(false)
}
array(3) {
  [0]=>
  string(15) "testT125748.php"
  [1]=>
  string(18) "--wiki=commonswiki"
  [2]=>
  string(6) "--help"
}
array(4) {
  [0]=>
  string(40) "/srv/mediawiki/multiversion/MWScript.php"
  [1]=>
  string(15) "testT125748.php"
  [2]=>
  string(11) "commonswiki"
  [3]=>
  string(6) "--help"
}
krenair@terbium:~$ sudo -u www-data php5 "/srv/mediawiki/multiversion/MWScript.php" testT125748.php commonswiki --help
array(0) {
}
array(3) {
  [0]=>
  string(15) "testT125748.php"
  [1]=>
  string(18) "--wiki=commonswiki"
  [2]=>
  string(6) "--help"
}
array(4) {
  [0]=>
  string(40) "/srv/mediawiki/multiversion/MWScript.php"
  [1]=>
  string(15) "testT125748.php"
  [2]=>
  string(11) "commonswiki"
  [3]=>
  string(6) "--help"
}

Docs for getopt say:

The parsing of options will end at the first non-option found, anything that follows is discarded.

So looks like PHP thinks testP2557.php is the first option. MWScript.php messes with $argv but I don't think PHP takes the data from this variable. From the code it looks like it uses $_SERVER['argv']. Also, looks like directly modifying it won't help much - it still uses the original value.

But HHVM just appears to use the global argv... which is more useful, imo.

Krenair renamed this task from PHP 5.5.9 seems to have issues parsing some command line parameters via multiversion's MWScript to PHP 5.5.9's getopt does not respect global $argv like HHVM does, causing issues parsing command line parameters via multiversion's MWScript..Feb 4 2016, 5:15 PM

Change 268328 had a related patch set uploaded (by Alex Monk):
Try to fix some other broken-looking legacy maintenance script options

https://gerrit.wikimedia.org/r/268328

Change 268328 merged by jenkins-bot:
Try to fix some other broken-looking legacy maintenance script options

https://gerrit.wikimedia.org/r/268328

tstarling closed this task as Resolved.Nov 14 2017, 10:03 AM
tstarling claimed this task.
tstarling added a subscriber: tstarling.

This was fixed by @Krenair by just not using getopt() anymore, which seems good enough to me. I confirmed that we're not using it, except in tests.