Skip to content

Commit b7f9df9

Browse files
authored
Merge pull request #1996 from BenBE/optarg-asserts
Properly handle potential NULL-deref issues
2 parents 086fd8e + 18fcb1a commit b7f9df9

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

CommandLine.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
189189
printVersionFlag(program);
190190
return STATUS_OK_EXIT;
191191
case 's':
192-
assert(optarg); /* please clang analyzer, cause optarg can be NULL in the 'u' case */
192+
if (!optarg)
193+
return STATUS_ERROR_EXIT;
194+
193195
if (String_eq(optarg, "help")) {
194196
for (int j = 1; j < LAST_PROCESSFIELD; j++) {
195197
const char* name = Process_fields[j].name;
@@ -214,6 +216,9 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
214216
}
215217
break;
216218
case 'd':
219+
if (!optarg)
220+
return STATUS_ERROR_EXIT;
221+
217222
if (sscanf(optarg, "%16d", &(flags->delay)) == 1) {
218223
if (flags->delay < 1)
219224
flags->delay = 1;
@@ -225,6 +230,9 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
225230
}
226231
break;
227232
case 'n':
233+
if (!optarg)
234+
return STATUS_ERROR_EXIT;
235+
228236
if (sscanf(optarg, "%16d", &flags->iterationsRemaining) == 1) {
229237
if (flags->iterationsRemaining <= 0) {
230238
fprintf(stderr, "Error: maximum iteration count must be positive.\n");
@@ -308,7 +316,9 @@ static CommandLineStatus parseArguments(int argc, char** argv, CommandLineSettin
308316
break;
309317
}
310318
case 'F':
311-
assert(optarg);
319+
if (!optarg)
320+
return STATUS_ERROR_EXIT;
321+
312322
if (optarg[0] == '\0' || optarg[0] == '|') {
313323
fprintf(stderr, "Error: invalid filter value \"%s\".\n", optarg);
314324
return STATUS_ERROR_EXIT;

0 commit comments

Comments
 (0)