From e251453e550e0b824ed6efe24215dcc4dfb86668 Mon Sep 17 00:00:00 2001 From: MCJack123 Date: Thu, 18 Apr 2024 19:22:20 -0400 Subject: [PATCH] Fixed bounds checking on computer IDs (fixes #350) --- src/main.cpp | 10 ++++++++-- src/peripheral/computer_p.cpp | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e4fa244f..9ab08140 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -542,8 +542,14 @@ int parseArguments(const std::vector& argv) { else if (arg == "--assets-dir" || arg == "-a") setROMPath(path_t(argv[++i])/"assets"/"computercraft"/"lua"); else if (arg.substr(0, 3) == "-a=") setROMPath(path_t(arg.substr(3))/"assets"/"computercraft"/"lua"); else if (arg == "--mc-save") computerDir = getMCSavePath() / argv[++i] / "computer"; - else if (arg == "-i" || arg == "--id") { manualID = true; id = std::stoi(argv[++i]); } - else if (arg == "--migrate") forceMigrate = true; + else if (arg == "-i" || arg == "--id") { + manualID = true; + try {id = std::stoi(argv[++i]);} + catch (std::out_of_range &e) { + std::cerr << "Error: Computer ID is out of range\n"; + return 1; + } + } else if (arg == "--migrate") forceMigrate = true; else if (arg == "--mount" || arg == "--mount-ro" || arg == "--mount-rw") { std::string mount_path = argv[++i]; if (mount_path.find('=') == std::string::npos) { diff --git a/src/peripheral/computer_p.cpp b/src/peripheral/computer_p.cpp index 111fe06c..d8e6b5df 100644 --- a/src/peripheral/computer_p.cpp +++ b/src/peripheral/computer_p.cpp @@ -54,7 +54,7 @@ computer::computer(lua_State *L, const char * side) { throw std::runtime_error("Computers are not available when using the Linux framebuffer"); if (strlen(side) < 10 || std::string(side).substr(0, 9) != "computer_" || (strlen(side) > 9 && !std::all_of(side + 9, side + strlen(side), ::isdigit))) throw std::invalid_argument("\"side\" parameter must be a number (the computer's ID)"); - int id = atoi(&side[9]); + int id = std::stoi(std::string(&side[9])); comp = NULL; { LockGuard lock(computers);