Handle null c strings correctly.

This commit is contained in:
James Pace 2022-04-15 01:50:26 +00:00
parent fb34bc2e19
commit 7ed92656fa
3 changed files with 21 additions and 7 deletions

View File

@ -27,3 +27,5 @@ std::string gen_token(
const std::string &priv_key, const std::string &priv_key,
const std::chrono::time_point<std::chrono::system_clock> &issue_time, const std::chrono::time_point<std::chrono::system_clock> &issue_time,
const std::chrono::time_point<std::chrono::system_clock> &expr_time); const std::chrono::time_point<std::chrono::system_clock> &expr_time);
std::string toString(const char* c_str);

View File

@ -53,10 +53,10 @@ int mosquitto_plugin_init(
std::filesystem::path aclFilePath; std::filesystem::path aclFilePath;
for (int index = 0; index < option_count; index++) for (int index = 0; index < option_count; index++)
{ {
const auto key = std::string(options[index].key); const auto key = toString(options[index].key);
if (key == "key_file") if (key == "key_file")
{ {
std::string key_file_string = std::string(options[index].value); std::string key_file_string = toString(options[index].value);
if (key_file_string.empty()) if (key_file_string.empty())
{ {
mosquitto_log_printf(MOSQ_LOG_ERR, "key_file not set."); mosquitto_log_printf(MOSQ_LOG_ERR, "key_file not set.");
@ -66,7 +66,7 @@ int mosquitto_plugin_init(
} }
else if (key == "acl_file") else if (key == "acl_file")
{ {
std::string acl_file_string = std::string(options[index].value); std::string acl_file_string = toString(options[index].value);
if (acl_file_string.empty()) if (acl_file_string.empty())
{ {
mosquitto_log_printf(MOSQ_LOG_ERR, "acl_file not set."); mosquitto_log_printf(MOSQ_LOG_ERR, "acl_file not set.");
@ -124,11 +124,11 @@ int j7s_auth_basic_auth_callback(int event, void *event_data, void *userdata)
if (!auth_data->password) if (!auth_data->password)
{ {
authorizer->add_unknown(std::string(auth_data->username)); authorizer->add_unknown(toString(auth_data->username));
return MOSQ_ERR_PLUGIN_DEFER; return MOSQ_ERR_PLUGIN_DEFER;
} }
bool is_authed = bool is_authed =
authorizer->add(std::string(auth_data->password), std::string(auth_data->username)); authorizer->add(toString(auth_data->password), toString(auth_data->username));
if (is_authed) if (is_authed)
{ {
@ -150,7 +150,7 @@ int j7s_acl_check_callback(int event, void *event_data, void *userdata)
struct mosquitto_evt_acl_check *acl_data = struct mosquitto_evt_acl_check *acl_data =
static_cast<struct mosquitto_evt_acl_check *>(event_data); static_cast<struct mosquitto_evt_acl_check *>(event_data);
const std::string username = std::string(mosquitto_client_username(acl_data->client)); const std::string username = toString(mosquitto_client_username(acl_data->client));
if (authorizer->is_unknown(username)) if (authorizer->is_unknown(username))
{ {
@ -179,7 +179,7 @@ int j7s_disconnect_callback(int event, void *event_data, void *userdata)
struct mosquitto_evt_disconnect *disconnect_data = struct mosquitto_evt_disconnect *disconnect_data =
static_cast<struct mosquitto_evt_disconnect *>(event_data); static_cast<struct mosquitto_evt_disconnect *>(event_data);
const std::string username = std::string(mosquitto_client_username(disconnect_data->client)); const std::string username = toString(mosquitto_client_username(disconnect_data->client));
authorizer->logout(username); authorizer->logout(username);
return MOSQ_ERR_SUCCESS; return MOSQ_ERR_SUCCESS;

View File

@ -119,3 +119,15 @@ std::string gen_token(
return token; return token;
} }
std::string toString(const char* c_str)
{
if(c_str)
{
return std::string(c_str);
}
else
{
return std::string();
}
}