aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Waterman <andrew@sifive.com>2023-02-03 13:34:59 -0800
committerAndrew Waterman <andrew@sifive.com>2023-02-03 14:01:34 -0800
commitdb65d052a89b85ee3be89fe5ea761e1752aaaad2 (patch)
tree02db407b4d7cdee4527d70f95024436b40d2f9da
parentf4035e2d87358f97a50e61a5edc6055f77e88219 (diff)
downloadspike-db65d052a89b85ee3be89fe5ea761e1752aaaad2.zip
spike-db65d052a89b85ee3be89fe5ea761e1752aaaad2.tar.gz
spike-db65d052a89b85ee3be89fe5ea761e1752aaaad2.tar.bz2
Improve input validation for --hartids flag
Disallow negative hartids, repeated hartids, and empty lists.
-rw-r--r--spike_main/spike.cc18
1 files changed, 18 insertions, 0 deletions
diff --git a/spike_main/spike.cc b/spike_main/spike.cc
index 9032db4..e7df0e0 100644
--- a/spike_main/spike.cc
+++ b/spike_main/spike.cc
@@ -299,10 +299,28 @@ static std::vector<int> parse_hartids(const char *s)
int n;
while (stream >> n) {
+ if (n < 0) {
+ fprintf(stderr, "Negative hart ID %d is unsupported\n", n);
+ exit(-1);
+ }
+
hartids.push_back(n);
if (stream.peek() == ',') stream.ignore();
}
+ if (hartids.empty()) {
+ fprintf(stderr, "No hart IDs specified\n");
+ exit(-1);
+ }
+
+ std::sort(hartids.begin(), hartids.end());
+
+ const auto dup = std::adjacent_find(hartids.begin(), hartids.end());
+ if (dup != hartids.end()) {
+ fprintf(stderr, "Duplicate hart ID %d\n", *dup);
+ exit(-1);
+ }
+
return hartids;
}