Fix crash on unknown commands (cf==nullptr)

This commit is contained in:
MichaelDvP
2021-07-22 19:35:27 +02:00
parent f2e0b193af
commit 6d94335079
4 changed files with 20 additions and 18 deletions

View File

@@ -38,13 +38,13 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
auto cf = find_command(device_type, cmd_new, id_new);
if ((cf == nullptr) || (cf->cmdfunction_json_)) {
LOG_WARNING(F("Command %s on %s not found"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
return 2; // command not found
return CommandRet::NOT_FOUND;
}
// check if we're allowed to call it
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
return 4; // command not allowed
return CommandRet::NOT_ALLOWED;
}
std::string dname = EMSdevice::device_type_2_device_name(device_type);
@@ -56,7 +56,7 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
LOG_INFO(F("Calling %s command '%s', value %s, id is %d"), dname.c_str(), cmd, value, id);
}
return ((cf->cmdfunction_)(value, id_new));
return ((cf->cmdfunction_)(value, id_new)) ? CommandRet::OK : CommandRet::ERROR;
}
// calls a command. Takes a json object for output.
@@ -70,9 +70,11 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
auto cf = find_command(device_type, cmd_new, id_new);
// check if we're allowed to call it
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated) {
if (cf != nullptr) {
if (cf->has_flags(CommandFlag::ADMIN_ONLY) && !authenticated && value != nullptr) {
LOG_WARNING(F("Command %s on %s not permitted. requires admin."), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
return 4; // command not allowed
return CommandRet::NOT_ALLOWED; // command not allowed
}
}
std::string dname = EMSdevice::device_type_2_device_name(device_type);
@@ -87,21 +89,21 @@ uint8_t Command::call(const uint8_t device_type, const char * cmd, const char *
// check if json object is empty, if so quit
if (json.isNull()) {
LOG_WARNING(F("Ignore call for command %s in %s because it has no json body"), cmd, EMSdevice::device_type_2_device_name(device_type).c_str());
return 3;
return CommandRet::ERROR;
}
// this is for endpoints that don't have commands, i.e not writable (e.g. boiler/syspress)
if (cf == nullptr) {
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type);
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type) ? CommandRet::OK : CommandRet::ERROR;
}
if (cf->cmdfunction_json_) {
return ((cf->cmdfunction_json_)(value, id_new, json));
return ((cf->cmdfunction_json_)(value, id_new, json)) ? CommandRet::OK : CommandRet::ERROR;
} else {
if ((device_type != EMSdevice::DeviceType::SYSTEM) && (value == nullptr || strlen(value) == 0 || strcmp(value, "?") == 0 || strcmp(value, "*") == 0)) {
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type);
return EMSESP::get_device_value_info(json, cmd_new, id_new, device_type) ? CommandRet::OK : CommandRet::ERROR;
}
return ((cf->cmdfunction_)(value, id_new));
return ((cf->cmdfunction_)(value, id_new)) ? CommandRet::OK : CommandRet::ERROR;
}
}

View File

@@ -413,7 +413,7 @@ void EMSESPShell::add_console_commands() {
shell.println(F("Unknown command"));
shell.print(F("Available commands are: "));
Command::show(shell, device_type, false); // non-verbose mode
} else if (cmd_return == CommandRet::ERROR) {
} else if (cmd_return != CommandRet::OK) {
shell.println(F("Bad syntax"));
}
},

View File

@@ -351,7 +351,7 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len)
if (cmd_return == CommandRet::NOT_FOUND) {
LOG_ERROR(F("No matching cmd (%s) in topic %s"), cmd_only, topic);
Mqtt::publish(F_(response), "unknown");
} else if (cmd_return == CommandRet::ERROR) {
} else if (cmd_return != CommandRet::OK) {
LOG_ERROR(F("Invalid data with cmd (%s) in topic %s"), cmd_only, topic);
Mqtt::publish(F_(response), "unknown");
}
@@ -405,7 +405,7 @@ void Mqtt::on_message(const char * fulltopic, const char * payload, size_t len)
if (cmd_return == CommandRet::NOT_FOUND) {
LOG_ERROR(F("No matching cmd (%s)"), command);
Mqtt::publish(F_(response), "unknown");
} else if (cmd_return == CommandRet::ERROR) {
} else if (cmd_return != CommandRet::OK) {
LOG_ERROR(F("Invalid data for cmd (%s)"), command);
Mqtt::publish(F_(response), "unknown");
}

View File

@@ -186,14 +186,14 @@ void WebAPIService::parse(AsyncWebServerRequest * request, std::string & device_
delete response;
send_message_response(request, 400, "Command not found"); // Bad Request
return;
} else if (cmd_reply == CommandRet::ERROR) {
delete response;
send_message_response(request, 400, "Problems parsing elements"); // Bad Request
return;
} else if (cmd_reply == CommandRet::NOT_ALLOWED) {
delete response;
send_message_response(request, 401, "Bad credentials"); // Unauthorized
return;
} else if (cmd_reply != CommandRet::OK) {
delete response;
send_message_response(request, 400, "Problems parsing elements"); // Bad Request
return;
}
if (!json.size()) {