1
0
mirror of synced 2025-01-19 01:24:15 +01:00

fix: Undefined behaviour when sending empty arguments to main instance

This commit is contained in:
WerWolv 2024-06-20 11:09:57 +02:00
parent ca5763650b
commit 3bfb0096e6
2 changed files with 18 additions and 12 deletions

View File

@ -20,6 +20,7 @@ namespace hex::subcommands {
}
}
}
return std::nullopt;
}
@ -27,7 +28,8 @@ namespace hex::subcommands {
// If no arguments, do not even try to process arguments
// (important because this function will exit ImHex if an instance is already opened,
// and we don't want that if no arguments were provided)
if (args.empty()) return;
if (args.empty())
return;
std::vector<std::pair<SubCommand, std::vector<std::string>>> subCommands;
@ -76,18 +78,18 @@ namespace hex::subcommands {
}
// Save last command to run
if (currentSubCommand) {
if (currentSubCommand.has_value()) {
subCommands.emplace_back(*currentSubCommand, currentSubCommandArgs);
}
// Run the subcommands
for (auto& subCommandPair : subCommands) {
subCommandPair.first.callback(subCommandPair.second);
for (auto &[subcommand, args] : subCommands) {
subcommand.callback(args);
}
// Exit the process if its not the main instance (the commands have been forwarded to another instance)
// Exit the process if it's not the main instance (the commands have been forwarded to another instance)
if (!ImHexApi::System::isMainInstance()) {
exit(0);
std::exit(0);
}
}
@ -95,12 +97,15 @@ namespace hex::subcommands {
log::debug("Forwarding subcommand {} (maybe to us)", cmdName);
std::vector<u8> data;
for (const auto &arg: args) {
data.insert(data.end(), arg.begin(), arg.end());
data.push_back('\0');
if (!args.empty()) {
for (const auto &arg: args) {
data.insert(data.end(), arg.begin(), arg.end());
data.push_back('\0');
}
data.pop_back();
}
data.erase(data.end()-1);
SendMessageToMainInstance::post(hex::format("command/{}", cmdName), data);
}

View File

@ -15,12 +15,13 @@ namespace hex::messaging {
void setupEvents() {
SendMessageToMainInstance::subscribe([](const std::string &eventName, const std::vector<u8> &eventData) {
log::debug("Forwarding message {} (maybe to us)", eventName);
if (ImHexApi::System::isMainInstance()) {
log::debug("Executing message '{}' in current instance", eventName);
EventImHexStartupFinished::subscribe([eventName, eventData]{
ImHexApi::Messaging::impl::runHandler(eventName, eventData);
});
} else {
log::debug("Forwarding message '{}' to existing instance", eventName);
sendToOtherInstance(eventName, eventData);
}
});