Skip to content

Commit 00f5ab4

Browse files
author
Michael Vasseur
committed
Remove FIXME as PHP implemented the return type
The documentation on this is not very clear. You can encounter the `False` by using unset($argv) which would be a misconfiguration so the error message is now also improved. A better explanation can be found here: https://bugs.php.net/bug.php?id=81352 Which has those comments; ``` [2021-08-12 15:19 UTC] [email protected] Prior to PHP 8.0.0, FALSE was returned if the function was called with unexpected parameters or parameter types. Most other functions returned NULL in that case. Anyhow, there is no need to document that, since this is actually undefined behavior[1]. For all relevant PHP versions, FALSE will also be returned, if neither $_SERVER nor $argv exists or are not arrays. [1] <https://www.php.net/manual/en/functions.internal.php> [2022-11-26 06:42 UTC] schamberumarcelo at gmail dot com getopt() will return an empty array if there is no error in splitting strings to args variable. GetOptions() will return a true value if the command line could be processed successfully. Otherwise, it will write error messages using die() and warn(). (https://www.imyccpay.com)github.com ``` given that we run PHP>8.0 and the explanation that parsing will stop for the first non-option I think we'll never encounter the false in practice. To be save we now check the shortoptions string for the right format. To be sure an internal function is now added which does some extra parsing to find unknown arguments, alternative would be to use: (https://www.php.net/manual/en/function.getopt.php#83414).
1 parent 1885c3f commit 00f5ab4

File tree

1 file changed

+47
-6
lines changed

1 file changed

+47
-6
lines changed

judge/judgedaemon.main.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,52 @@
1616
$endpoints = [];
1717
$domjudge_config = [];
1818

19+
function dj_getopt(string $short_options, array $long_options = []): array
20+
{
21+
global $argv;
22+
define('GETOPT_REGEX', "/^([a-zA-Z0-9]:{0,2})*$/");
23+
if (preg_match(GETOPT_REGEX, $short_options) !== 1) {
24+
echo "Error: short options format specified is invalid.\n";
25+
usage();
26+
}
27+
$options = getopt($short_options, $long_options);
28+
if ($options===false || !is_array($argv)) {
29+
echo "Error: parsing options failed.\nPlease check: `register_argc_arg` in php.ini.\n";
30+
usage();
31+
}
32+
$unknown_option = false;
33+
foreach (array_slice($argv, 1) as $arg) {
34+
if ($arg === '--') {
35+
// By convention we can stop processing arguments.
36+
break;
37+
}
38+
if (str_starts_with($arg, '--')) {
39+
foreach($long_options as $long_option) {
40+
$stripped_arg = substr($arg, 2);
41+
if (str_starts_with($long_option, $stripped_arg) && $stripped_arg !== $long_option) {
42+
echo "Error: Shorten long options is not supported: $arg\n";
43+
$unknown_option = true;
44+
} elseif (!array_key_exists($stripped_arg, $options)) {
45+
echo "Error: Unknown option: $arg\n";
46+
$unknown_option = true;
47+
}
48+
}
49+
} elseif (str_starts_with($arg, '-')) {
50+
foreach (str_split(ltrim($arg, '-')) as $c) {
51+
if (!array_key_exists($c, $options)) {
52+
echo "Error: Unknown option: $arg\n";
53+
$unknown_option = true;
54+
}
55+
}
56+
}
57+
}
58+
if ($unknown_option) {
59+
echo "\n";
60+
usage();
61+
}
62+
return $options;
63+
}
64+
1965
function judging_directory(string $workdirpath, array $judgeTask) : string
2066
{
2167
if (filter_var($judgeTask['submitid'], FILTER_VALIDATE_INT) === false ||
@@ -487,12 +533,7 @@ function fetch_executable_internal(
487533
return [$execrunpath, null, null];
488534
}
489535

490-
$options = getopt("dv:n:hVe:j:t:", ["diskspace-error"]);
491-
// FIXME: getopt doesn't return FALSE on parse failure as documented!
492-
if ($options===false) {
493-
echo "Error: parsing options failed.\n";
494-
usage();
495-
}
536+
$options = dj_getopt("dv:n:hVe:j:t:", ["diskspace-error"]);
496537
if (isset($options['v'])) {
497538
$options['verbose'] = $options['v'];
498539
}

0 commit comments

Comments
 (0)