diff --git a/slsDetectorSoftware/src/CmdParser.cpp b/slsDetectorSoftware/src/CmdParser.cpp index d036e4dc2..bdfcc5c40 100644 --- a/slsDetectorSoftware/src/CmdParser.cpp +++ b/slsDetectorSoftware/src/CmdParser.cpp @@ -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(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()) + command_ = *it++; // First arg is the comand to run arguments_ = std::vector(it, std::istream_iterator()); diff --git a/slsDetectorSoftware/tests/test-CmdParser.cpp b/slsDetectorSoftware/tests/test-CmdParser.cpp index cec60358e..e2174fd95 100644 --- a/slsDetectorSoftware/tests/test-CmdParser.cpp +++ b/slsDetectorSoftware/tests/test-CmdParser.cpp @@ -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 \ No newline at end of file