Skip to content

Commit 48b887f

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. Check the return code for system commands Those were left over FIXMEs We will only terminate on either: - failure in startup - when communiation with the domserver fails as we can't communicate at that point. Implement Tobi his feedback
1 parent f3409e5 commit 48b887f

File tree

2 files changed

+68
-11
lines changed

2 files changed

+68
-11
lines changed

judge/judgedaemon.main.php

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,72 @@
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+
41+
$t_argv = $argv;
42+
43+
// The first is the script name
44+
$t_argc = $argc-1;
45+
unset($t_argv[0]);
46+
$t_argv = array_values($t_argv);
47+
48+
foreach ($options as $option_key => $option_value) {
49+
$short = '-'.$option_key;
50+
$long = '--'.$option_key;
51+
$possible = [$short, $long];
52+
53+
$values = [];
54+
if ($option_value !== false) {
55+
if (is_string($option_value)) {
56+
$values = [$option_value];
57+
} else {
58+
$values = $option_value;
59+
}
60+
foreach ($values as $value) {
61+
// The value can be appended to the short option
62+
$possible[] = $short.$value;
63+
}
64+
}
65+
66+
$t_remove = [];
67+
for ($i = 0; $i < $t_argc; $i++) {
68+
if (in_array($t_argv[$i], array_merge($possible, $values))) {
69+
$t_remove[] = $i;
70+
}
71+
}
72+
foreach ($t_remove as $t) {
73+
unset($t_argv[$t]);
74+
}
75+
$t_argv = array_values($t_argv);
76+
$t_argc -= count($t_remove);
77+
}
78+
79+
if (count($t_argv) !== 0) {
80+
echo "Error: found unknown arguments: '" . implode(' ', $t_argv) . "'.\n";
81+
usage();
82+
}
83+
}
84+
1985
function judging_directory(string $workdirpath, array $judgeTask) : string
2086
{
2187
if (filter_var($judgeTask['submitid'], FILTER_VALIDATE_INT) === false ||
@@ -487,16 +553,7 @@ function fetch_executable_internal(
487553
return [$execrunpath, null, null];
488554
}
489555

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

webapp/src/Controller/API/JudgehostController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ 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+
if (in_array($disabled['kind'], ['compile_script', 'compare_script', 'run_script', 'debug_script'])) {
748748
$field_name = $disabled['kind'] . '_id';
749749
$disabled_id = $disabled[$field_name];
750750

0 commit comments

Comments
 (0)