Skip to content

Commit 64f40b5

Browse files
author
Michael Vasseur
committed
Add function to parse getopt in a less buggy way
Not sure if it's worth the extra code but fixes all found issues from the reviews.
1 parent 746c84e commit 64f40b5

File tree

2 files changed

+67
-11
lines changed

2 files changed

+67
-11
lines changed

judge/judgedaemon.main.php

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,70 @@
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+
$options = getopt($short_options, $long_options);
24+
if (preg_match(GETOPT_REGEX, $short_options) !== 1) {
25+
echo "Error: short options format specified is invalid.\n";
26+
usage();
27+
}
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+
check_unknown_cli_options($options);
33+
return $options;
34+
}
35+
36+
function check_unknown_cli_options(array $options): void
37+
{
38+
global $argc;
39+
global $argv;
40+
// Cleanup the original arguments
41+
$t_argv = $argv;
42+
// The first is the script name
43+
$t_argc = $argc-1;
44+
unset($t_argv[0]);
45+
$t_argv = array_values($t_argv);
46+
47+
foreach ($options as $option_key => $option_value) {
48+
$short = '-'.$option_key;
49+
$long = '--'.$option_key;
50+
$possible = [$short, $long];
51+
52+
$values = [];
53+
if ($option_value !== false) {
54+
if (is_string($option_value)) {
55+
$values = [$option_value];
56+
} else {
57+
$values = $option_value;
58+
}
59+
foreach ($values as $value) {
60+
$possible[] = $short.$value;
61+
}
62+
}
63+
64+
$t_remove = [];
65+
for ($i = 0; $i < $t_argc; $i++) {
66+
if (in_array($t_argv[$i], array_merge($possible, $values))) {
67+
$t_remove[] = $i;
68+
}
69+
}
70+
foreach ($t_remove as $t) {
71+
unset($t_argv[$t]);
72+
}
73+
$t_argv = array_values($t_argv);
74+
$t_argc -= count($t_remove);
75+
}
76+
77+
if (count($t_argv) !== 0) {
78+
echo "Error: found unknown arguments: '" . implode(' ', $t_argv) . "'.\n";
79+
usage();
80+
}
81+
}
82+
1983
function judging_directory(string $workdirpath, array $judgeTask) : string
2084
{
2185
if (filter_var($judgeTask['submitid'], FILTER_VALIDATE_INT) === false ||
@@ -483,16 +547,7 @@ function fetch_executable_internal(
483547
return [$execrunpath, null, null];
484548
}
485549

486-
$shortoptions = "dv:n:hVe:j:t:";
487-
$regex = "/^([a-zA-Z0-9]:{0,2})*$/";
488-
if (preg_match($regex, $shortoptions) !== 1) {
489-
echo "Error: short options format specified is invalid.\n";
490-
}
491-
$options = getopt($shortoptions, ["diskspace-error"]);
492-
if ($options===false || !is_array($argv)) {
493-
echo "Error: parsing options failed.\nPlease check: `register_argc_arg` in php.ini.\n";
494-
usage();
495-
}
550+
$options = dj_getopt("dv:n:hVe:j:t:", ["diskspace-error"]);
496551
if (isset($options['v'])) {
497552
$options['verbose'] = $options['v'];
498553
}

webapp/src/Controller/API/JudgehostController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,8 @@ public function internalErrorAction(Request $request): ?int
744744

745745
$field_name = null;
746746
$disabled_id = null;
747-
if (in_array($disabled['kind'], ['compile_script', 'compare_script', 'run_script'])) {
747+
var_dump($disabled);
748+
if (in_array($disabled['kind'], ['compile_script', 'compare_script', 'run_script', 'debug_script'])) {
748749
$field_name = $disabled['kind'] . '_id';
749750
$disabled_id = $disabled[$field_name];
750751

0 commit comments

Comments
 (0)