From ee98e68d1e23e7974d7378db1d976069df8a07ef Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Wed, 1 Sep 2021 10:27:30 +0200 Subject: [PATCH] Updated string to integer conversions --- src/raspberrypi/rascsi.cpp | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/raspberrypi/rascsi.cpp b/src/raspberrypi/rascsi.cpp index a5560ffe..a8d32cf5 100644 --- a/src/raspberrypi/rascsi.cpp +++ b/src/raspberrypi/rascsi.cpp @@ -253,11 +253,20 @@ void Reset() bus->Reset(); } -//--------------------------------------------------------------------------- -// -// Get the list of attached devices -// -//--------------------------------------------------------------------------- +bool GetAsInt(const string& value, int& result) +{ + try { + result = std::stoul(value); + } + catch(const invalid_argument& e) { + return false; + } + catch(const out_of_range& e) { + return false; + } + + return true; +} void GetImageFile(PbImageFile *image_file, const string& filename) { @@ -1013,15 +1022,12 @@ bool ProcessCmd(const int fd, const PbCommand& command) else if (command.operation() == RESERVE) { set reserved; for (int i = 0; i < command.params_size(); i++) { - try { - reserved.insert(std::stoi(command.params(i))); - } - catch(const invalid_argument& e) { - return ReturnStatus(fd, false, "Invalid ID " + command.params(i)); - } - catch(const out_of_range& e) { + int id; + if (!GetAsInt(command.params(i), id)) { return ReturnStatus(fd, false, "Invalid ID " + command.params(i)); } + + reserved.insert(id); } reserved_ids = reserved; @@ -1098,7 +1104,9 @@ bool ParseArgument(int argc, char* argv[], int& port) continue; case 'b': { - block_size = atoi(optarg); + if (!GetAsInt(optarg, block_size)) { + cerr << "Invalid block size " << optarg << endl; + } continue; } @@ -1124,8 +1132,7 @@ bool ParseArgument(int argc, char* argv[], int& port) continue; case 'p': - port = atoi(optarg); - if (port <= 0 || port > 65535) { + if (!GetAsInt(optarg, port) || port <= 0 || port > 65535) { cerr << "Invalid port " << optarg << ", port must be between 1 and 65535" << endl; return false; }