Skip to content

Commit

Permalink
Fixed bounds checking on computer IDs (fixes #350)
Browse files Browse the repository at this point in the history
  • Loading branch information
MCJack123 committed Apr 18, 2024
1 parent de14d9a commit e251453
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/main.cpp
Expand Up @@ -542,8 +542,14 @@ int parseArguments(const std::vector<std::string>& 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/peripheral/computer_p.cpp
Expand Up @@ -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);
Expand Down

0 comments on commit e251453

Please sign in to comment.