Skip to content

Commit 1bd62d8

Browse files
committed
Don't yolo numerical command line arguments: complain if not numeric.
1 parent b8a81e8 commit 1bd62d8

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

bant/bant.cc

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ int getopt(int, char *const *, const char *); // NOLINT
4040
#include <vector>
4141

4242
#include "absl/container/flat_hash_set.h"
43+
#include "absl/strings/numbers.h"
4344
#include "bant/cli-commands.h"
4445
#include "bant/output-format.h"
4546
#include "bant/session.h"
@@ -237,9 +238,12 @@ int main(int argc, char *argv[]) {
237238
break;
238239

239240
case 'r':
240-
flags.recurse_dependency_depth = optarg //
241-
? atoi(optarg)
242-
: std::numeric_limits<int>::max();
241+
flags.recurse_dependency_depth = std::numeric_limits<int>::max();
242+
if (optarg &&
243+
!absl::SimpleAtoi(optarg, &flags.recurse_dependency_depth)) {
244+
return usage(argv[0], "-r requires either none or a numeric parameter",
245+
EXIT_FAILURE);
246+
}
243247
break;
244248

245249
case 'k': flags.ignore_keep_comment = true; break;
@@ -262,7 +266,11 @@ int main(int argc, char *argv[]) {
262266
}
263267
flags.output_format = found->second;
264268
} break;
265-
case 'T': flags.io_threads = atoi(optarg); break;
269+
case 'T':
270+
if (!absl::SimpleAtoi(optarg, &flags.io_threads)) {
271+
return usage(argv[0], "-T needs a numeric parameter", EXIT_FAILURE);
272+
}
273+
break;
266274
case 'v': flags.verbose++; break; // More -v, more detail.
267275
case 'V': return print_version();
268276
default: return usage(argv[0], nullptr, EXIT_SUCCESS);

0 commit comments

Comments
 (0)