Skip to content

Commit b87c71d

Browse files
committed
Coverity: Now we have optional flags, make sure to validate optarg
In theory getopt_long() should do this for us, but Coverity doesn't understand it. What it does understand is that sometimes we check something for a null - we should check it all the time. Make it so. Signed-off-by: Robin Getz <[email protected]>
1 parent c9250c0 commit b87c71d

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

tests/iio_adi_xflow_check.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,10 @@ int main(int argc, char **argv)
186186
break;
187187

188188
case 's':
189+
if (!optarg) {
190+
fprintf(stderr, "Samples options requires a value.\n\n");
191+
return EXIT_FAILURE;
192+
}
189193
ret = sscanf(optarg, "%u%c", &buffer_size, &unit);
190194
if (ret == 0)
191195
return EXIT_FAILURE;

tests/iio_attr.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,10 @@ int main(int argc, char **argv)
411411
quiet = true;
412412
break;
413413
case 'g':
414+
if (!optarg) {
415+
fprintf(stderr, "Code generation requires an option\n");
416+
return EXIT_FAILURE;
417+
}
414418
gen_code = true;
415419
gen_file = optarg;
416420
break;

tests/iio_common.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
232232
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
233233
return NULL;
234234
}
235+
if (!optarg) {
236+
fprintf(stderr, "network options requires a uri\n");
237+
return NULL;
238+
}
235239
backend = IIO_NETWORK;
236240
arg = optarg;
237241
break;
@@ -240,6 +244,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
240244
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
241245
return NULL;
242246
}
247+
if (!optarg) {
248+
fprintf(stderr, "xml options requires a uri\n");
249+
return NULL;
250+
}
243251
backend = IIO_XML;
244252
arg = optarg;
245253
break;
@@ -248,6 +256,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
248256
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
249257
return NULL;
250258
}
259+
if (!optarg) {
260+
fprintf(stderr, "uri options requires a uri\n");
261+
return NULL;
262+
}
251263
backend = IIO_AUTO;
252264
arg = optarg;
253265
break;

tests/iio_readdev.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,15 +219,31 @@ int main(int argc, char **argv)
219219
optind++;
220220
break;
221221
case 't':
222+
if (!optarg) {
223+
fprintf(stderr, "Trigger requires an argument\n");
224+
return EXIT_FAILURE;
225+
}
222226
trigger_name = optarg;
223227
break;
224228
case 'b':
229+
if (!optarg) {
230+
fprintf(stderr, "Buffersize requires an argument\n");
231+
return EXIT_FAILURE;
232+
}
225233
buffer_size = sanitize_clamp("buffer size", optarg, 64, 4 * 1024 * 1024);
226234
break;
227235
case 's':
236+
if (!optarg) {
237+
fprintf(stderr, "Number of Samples requires an argument\n");
238+
return EXIT_FAILURE;
239+
}
228240
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
229241
break;
230242
case 'T':
243+
if (!optarg) {
244+
fprintf(stderr, "Timeout requires an argument\n");
245+
return EXIT_FAILURE;
246+
}
231247
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
232248
break;
233249
case '?':

tests/iio_writedev.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,15 +230,31 @@ int main(int argc, char **argv)
230230
optind++;
231231
break;
232232
case 't':
233+
if (!optarg) {
234+
fprintf(stderr, "Trigger requires argument\n");
235+
return EXIT_FAILURE;
236+
}
233237
trigger_name = optarg;
234238
break;
235239
case 'b':
240+
if (!optarg) {
241+
fprintf(stderr, "Buffer Size requires argument\n");
242+
return EXIT_FAILURE;
243+
}
236244
buffer_size = sanitize_clamp("buffer size", optarg, 64, 4 * 1024 * 1024);
237245
break;
238246
case 's':
247+
if (!optarg) {
248+
fprintf(stderr, "Number of samples requires argument\n");
249+
return EXIT_FAILURE;
250+
}
239251
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
240252
break;
241253
case 'T':
254+
if (!optarg) {
255+
fprintf(stderr, "Timeout requires argument\n");
256+
return EXIT_FAILURE;
257+
}
242258
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
243259
break;
244260
case 'c':

0 commit comments

Comments
 (0)