From 833acefbb372cb3046f8d9fc8b15f2a66bdcae33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Monta=C3=B1ana=20G=C3=B3mez?= Date: Sun, 22 Oct 2023 20:21:50 +0200 Subject: [PATCH] Fix index limits mistake in manage --- src/Platform/CommandParser.cc | 10 +++++++++- src/Platform/CommandParser.h | 2 +- src/Platform/ManageResults.cc | 8 ++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Platform/CommandParser.cc b/src/Platform/CommandParser.cc index 5123e66..30fc272 100644 --- a/src/Platform/CommandParser.cc +++ b/src/Platform/CommandParser.cc @@ -10,7 +10,7 @@ namespace platform { { cout << Colors::RED() << message << Colors::RESET() << endl; } - pair CommandParser::parse(const string& color, const vector>& options, const char defaultCommand) + pair CommandParser::parse(const string& color, const vector>& options, const char defaultCommand, const int maxIndex) { bool finished = false; while (!finished) { @@ -36,6 +36,10 @@ namespace platform { if (all_of(line.begin(), line.end(), ::isdigit)) { command = defaultCommand; index = stoi(line); + if (index > maxIndex || index < 0) { + messageError("Index out of range"); + continue; + } finished = true; break; } @@ -54,6 +58,10 @@ namespace platform { } try { index = stoi(line); + if (index > maxIndex || index < 0) { + messageError("Index out of range"); + break; + } } catch (const std::invalid_argument& ia) { messageError("Invalid value: " + line); diff --git a/src/Platform/CommandParser.h b/src/Platform/CommandParser.h index 1e71d34..0667357 100644 --- a/src/Platform/CommandParser.h +++ b/src/Platform/CommandParser.h @@ -9,7 +9,7 @@ namespace platform { class CommandParser { public: CommandParser() = default; - pair parse(const string& color, const vector>& options, const char defaultCommand); + pair parse(const string& color, const vector>& options, const char defaultCommand, const int maxIndex); char getCommand() const { return command; }; int getIndex() const { return index; }; private: diff --git a/src/Platform/ManageResults.cc b/src/Platform/ManageResults.cc index 39652db..83d92e2 100644 --- a/src/Platform/ManageResults.cc +++ b/src/Platform/ManageResults.cc @@ -98,10 +98,6 @@ namespace platform { { // Show a dataset result inside a report auto data = results.at(index).load(); - if (idx < 0 or idx >= static_cast(data["results"].size())) { - cout << "Invalid index" << endl; - return; - } cout << Colors::YELLOW() << "Showing " << results.at(index).getFilename() << endl; ReportConsole reporter(data, compare, idx); reporter.show(); @@ -160,9 +156,9 @@ namespace platform { auto parser = CommandParser(); while (!finished) { if (indexList) { - tie(option, index) = parser.parse(Colors::GREEN(), mainOptions, 'r'); + tie(option, index) = parser.parse(Colors::GREEN(), mainOptions, 'r', numFiles - 1); } else { - tie(option, subIndex) = parser.parse(Colors::MAGENTA(), listOptions, 'r'); + tie(option, subIndex) = parser.parse(Colors::MAGENTA(), listOptions, 'r', results.at(index).load()["results"].size() - 1); } switch (option) { case 'q':