Skip to content

Commit

Permalink
Coverity: Now we have optional flags, make sure to validate optarg
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rgetz committed Jun 2, 2020
1 parent c9250c0 commit b87c71d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tests/iio_adi_xflow_check.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ int main(int argc, char **argv)
break;

case 's':
if (!optarg) {
fprintf(stderr, "Samples options requires a value.\n\n");
return EXIT_FAILURE;
}
ret = sscanf(optarg, "%u%c", &buffer_size, &unit);
if (ret == 0)
return EXIT_FAILURE;
Expand Down
4 changes: 4 additions & 0 deletions tests/iio_attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,10 @@ int main(int argc, char **argv)
quiet = true;
break;
case 'g':
if (!optarg) {
fprintf(stderr, "Code generation requires an option\n");
return EXIT_FAILURE;
}
gen_code = true;
gen_file = optarg;
break;
Expand Down
12 changes: 12 additions & 0 deletions tests/iio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
}
if (!optarg) {
fprintf(stderr, "network options requires a uri\n");
return NULL;
}
backend = IIO_NETWORK;
arg = optarg;
break;
Expand All @@ -240,6 +244,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
}
if (!optarg) {
fprintf(stderr, "xml options requires a uri\n");
return NULL;
}
backend = IIO_XML;
arg = optarg;
break;
Expand All @@ -248,6 +256,10 @@ struct iio_context * handle_common_opts(char * name, int argc, char * const argv
fprintf(stderr, "-a, -x, -n and -u are mutually exclusive\n");
return NULL;
}
if (!optarg) {
fprintf(stderr, "uri options requires a uri\n");
return NULL;
}
backend = IIO_AUTO;
arg = optarg;
break;
Expand Down
16 changes: 16 additions & 0 deletions tests/iio_readdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -219,15 +219,31 @@ int main(int argc, char **argv)
optind++;
break;
case 't':
if (!optarg) {
fprintf(stderr, "Trigger requires an argument\n");
return EXIT_FAILURE;
}
trigger_name = optarg;
break;
case 'b':
if (!optarg) {
fprintf(stderr, "Buffersize requires an argument\n");
return EXIT_FAILURE;
}
buffer_size = sanitize_clamp("buffer size", optarg, 64, 4 * 1024 * 1024);
break;
case 's':
if (!optarg) {
fprintf(stderr, "Number of Samples requires an argument\n");
return EXIT_FAILURE;
}
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
break;
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires an argument\n");
return EXIT_FAILURE;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
case '?':
Expand Down
16 changes: 16 additions & 0 deletions tests/iio_writedev.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,31 @@ int main(int argc, char **argv)
optind++;
break;
case 't':
if (!optarg) {
fprintf(stderr, "Trigger requires argument\n");
return EXIT_FAILURE;
}
trigger_name = optarg;
break;
case 'b':
if (!optarg) {
fprintf(stderr, "Buffer Size requires argument\n");
return EXIT_FAILURE;
}
buffer_size = sanitize_clamp("buffer size", optarg, 64, 4 * 1024 * 1024);
break;
case 's':
if (!optarg) {
fprintf(stderr, "Number of samples requires argument\n");
return EXIT_FAILURE;
}
num_samples = sanitize_clamp("number of samples", optarg, 0, SIZE_MAX);
break;
case 'T':
if (!optarg) {
fprintf(stderr, "Timeout requires argument\n");
return EXIT_FAILURE;
}
timeout = sanitize_clamp("timeout", optarg, 0, INT_MAX);
break;
case 'c':
Expand Down

0 comments on commit b87c71d

Please sign in to comment.