fixing segfault on command without args

This commit is contained in:
Erik Fröjdh committed 2026-10-01 11:18:20 +02:00
1 parent 68628d975c
commit d2c3db5eeb
2 files changed
+64 -1

No files matched your search

+5 -1
View File
@@ -50,7 +50,11 @@ void CmdParser::Parse(std::string s) {
// Command and args should now be all that's left in the string
std::istringstream iss(s);
auto it = std::istream_iterator<std::string>(iss);
command_ = *it++; // First arg is the comand to run
// Only id/position (or whitespace) and no command leaves nothing to
// read, dereferencing the end-of-stream iterator is undefined behaviour
if (it != std::istream_iterator<std::string>())
command_ = *it++; // First arg is the comand to run
arguments_ =
std::vector<std::string>(it, std::istream_iterator<std::string>());
@@ -409,4 +409,63 @@ TEST_CASE("Parse a command in the form 0-1:command") {
REQUIRE(p.command() == "exptime");
}
TEST_CASE("Parse only det id without command gives empty command") {
CmdParser p;
p.Parse("7-");
REQUIRE(p.multi_id() == 7);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.receiver_id() == -1);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse only mod id without command gives empty command") {
CmdParser p;
p.Parse("0:");
REQUIRE(p.multi_id() == 0);
REQUIRE(p.detector_id() == 0);
REQUIRE(p.receiver_id() == -1);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse only mod and receiver id without command gives empty command") {
CmdParser p;
p.Parse("0:1");
REQUIRE(p.multi_id() == 0);
REQUIRE(p.detector_id() == 0);
REQUIRE(p.receiver_id() == 1);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse only whitespace gives empty command and default id") {
CmdParser p;
p.Parse(" ");
REQUIRE(p.multi_id() == 0);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.receiver_id() == -1);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse only --help without command gives empty command") {
CmdParser p;
p.Parse("--help");
REQUIRE(p.isHelp());
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse only id from argv without command gives empty command") {
int argc = 2;
const char *const argv[]{"caller", "7-"};
CmdParser p;
p.Parse(argc, argv);
REQUIRE(p.multi_id() == 7);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
} // namespace sls