[bigint][test] Parse command-line integer parameters robustly

The previous implementation would simply crash when command-line
parameters to the bigint_shell weren't valid integers (where
integers were expected).

Change-Id: Ia9987ab241ae9cac8dad4517766bfdd0b8cc98b7
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3677295
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Auto-Submit: Jakob Kummerow <jkummerow@chromium.org>
Reviewed-by: Maya Lekova <mslekova@chromium.org>
Commit-Queue: Maya Lekova <mslekova@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80824}
This commit is contained in:
Jakob Kummerow 2022-05-30 16:16:38 +02:00 committed by V8 LUCI CQ
parent ec8b247201
commit d1f2ccf15c

View File

@ -481,6 +481,20 @@ class Runner {
}
}
template <typename I>
bool ParseInt(char* s, I* out) {
char* end;
if (s[0] == '\0') return false;
errno = 0;
long l = strtol(s, &end, 10);
if (errno != 0 || *end != '\0' || l > std::numeric_limits<I>::max() ||
l < std::numeric_limits<I>::min()) {
return false;
}
*out = static_cast<I>(l);
return true;
}
int ParseOptions(int argc, char** argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--list") == 0) {
@ -490,14 +504,16 @@ class Runner {
return 0;
} else if (strcmp(argv[i], "--random-seed") == 0 ||
strcmp(argv[i], "--random_seed") == 0) {
random_seed_ = std::stoi(argv[++i]);
if (++i == argc || !ParseInt(argv[i], &random_seed_)) {
return PrintHelp(argv);
}
} else if (strncmp(argv[i], "--random-seed=", 14) == 0 ||
strncmp(argv[i], "--random_seed=", 14) == 0) {
random_seed_ = std::stoi(argv[i] + 14);
if (!ParseInt(argv[i] + 14, &random_seed_)) return PrintHelp(argv);
} else if (strcmp(argv[i], "--runs") == 0) {
runs_ = std::stoi(argv[++i]);
if (++i == argc || !ParseInt(argv[i], &runs_)) return PrintHelp(argv);
} else if (strncmp(argv[i], "--runs=", 7) == 0) {
runs_ = std::stoi(argv[i] + 7);
if (!ParseInt(argv[i] + 7, &runs_)) return PrintHelp(argv);
}
#define TEST(kName, name) \
else if (strcmp(argv[i], name) == 0) { \