Skip to content

Commit a542fb4

Browse files
galpeteryichoi
authored andcommitted
Correctly check number of cmd line arguments (#1002)
If the IoT.js binary is started with one of the `--memstat`, `--show-opcodes`, or `--start-debug-server` arguments the program crashes when the process module initilizes its argv array. The reason for the crash is that the number of arguments are checked before the previously mentioned arguments are processed and after they processed the environment's argc is set to 2, regardless that there is no valid filename/module argument. Test case: ``` ./iotjs --memstat ``` IoT.js-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent 4700966 commit a542fb4

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/iotjs_env.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,6 @@ bool iotjs_environment_parse_command_line_arguments(iotjs_environment_t* env,
9898
char** argv) {
9999
IOTJS_VALIDATED_STRUCT_METHOD(iotjs_environment_t, env);
100100

101-
// There must be at least two arguments.
102-
if (argc < 2) {
103-
fprintf(stderr,
104-
"Usage: iotjs [options] {script | script.js} [arguments]\n");
105-
return false;
106-
}
107-
108101
// Parse IoT.js command line arguments.
109102
uint32_t i = 1;
110103
while (i < argc && argv[i][0] == '-') {
@@ -121,6 +114,13 @@ bool iotjs_environment_parse_command_line_arguments(iotjs_environment_t* env,
121114
++i;
122115
}
123116

117+
// There must be at least one argument after processing the IoT.js args.
118+
if ((argc - i) < 1) {
119+
fprintf(stderr,
120+
"Usage: iotjs [options] {script | script.js} [arguments]\n");
121+
return false;
122+
}
123+
124124
// Remaining arguments are for application.
125125
_this->argc = 2;
126126
size_t buffer_size = ((size_t)(_this->argc + argc - i)) * sizeof(char*);

0 commit comments

Comments
 (0)