diff --git a/client/src/cmdmqtt.c b/client/src/cmdmqtt.c index f7927da74..51bae4cd6 100644 --- a/client/src/cmdmqtt.c +++ b/client/src/cmdmqtt.c @@ -305,24 +305,24 @@ static int CmdMqttSend(const char *Cmd) { } if (alen == 0) { - if (strlen(g_session.mqtt_server)) { - strcpy(addr, g_session.mqtt_server); + if ((g_session.mqtt_server != NULL) && (strcmp(g_session.mqtt_server, "") != 0)) { + snprintf(addr, sizeof(addr), "%s", g_session.mqtt_server); } else { strcpy(addr, "mqtt.proxdump.com"); } } if (plen == 0) { - if (strlen(g_session.mqtt_port)) { - strcpy(port, g_session.mqtt_port); + if ((g_session.mqtt_port != NULL) && (strcmp(g_session.mqtt_port, "") != 0)) { + snprintf(port, sizeof(port), "%s", g_session.mqtt_port); } else { strcpy(port, "1883"); } } if (tlen == 0) { - if (strlen(g_session.mqtt_topic)) { - strcpy(topic, g_session.mqtt_topic); + if ((g_session.mqtt_topic != NULL) && (strcmp(g_session.mqtt_topic, "") != 0)) { + snprintf(topic, sizeof(topic), "%s", g_session.mqtt_topic); } else { strcpy(topic, "proxdump"); } @@ -382,24 +382,24 @@ static int CmdMqttReceive(const char *Cmd) { } if (alen == 0) { - if (strlen(g_session.mqtt_server)) { - strcpy(addr, g_session.mqtt_server); + if ((g_session.mqtt_server != NULL) && (strcmp(g_session.mqtt_server, "") != 0)) { + snprintf(addr, sizeof(addr), "%s", g_session.mqtt_server); } else { strcpy(addr, "proxdump.com"); } } if (plen == 0) { - if (strlen(g_session.mqtt_port)) { - strcpy(port, g_session.mqtt_port); + if ((g_session.mqtt_port != NULL) && (strcmp(g_session.mqtt_port, "") != 0)) { + snprintf(port, sizeof(port), "%s", g_session.mqtt_port); } else { strcpy(port, "1883"); } } if (tlen == 0) { - if (strlen(g_session.mqtt_topic)) { - strcpy(topic, g_session.mqtt_topic); + if ((g_session.mqtt_topic != NULL) && (strcmp(g_session.mqtt_topic, "") != 0)) { + snprintf(topic, sizeof(topic), "%s", g_session.mqtt_topic); } else { strcpy(topic, "proxdump"); } diff --git a/client/src/preferences.c b/client/src/preferences.c index dafb7e7b9..5fd47fd0e 100644 --- a/client/src/preferences.c +++ b/client/src/preferences.c @@ -61,9 +61,9 @@ static bool setDefaultMqttServer(const char *srv) { char *tmp = (char *)realloc(g_session.mqtt_server, strlen(srv) + 1); if (tmp == NULL) { + // keep the old value. realloc leaves it valid on failure, and dropping it + // would hand every later reader a NULL PrintAndLogEx(WARNING, "Failed to allocate memory"); - free(g_session.mqtt_server); - g_session.mqtt_server = NULL; return false; } g_session.mqtt_server = tmp; @@ -84,9 +84,9 @@ static bool setDefaultMqttPort(const char *port) { char *tmp = (char *)realloc(g_session.mqtt_port, strlen(port) + 1); if (tmp == NULL) { + // keep the old value. realloc leaves it valid on failure, and dropping it + // would hand every later reader a NULL PrintAndLogEx(WARNING, "Failed to allocate memory"); - free(g_session.mqtt_port); - g_session.mqtt_port = NULL; return false; } g_session.mqtt_port = tmp; @@ -107,9 +107,9 @@ static bool setDefaultMqttTopic(const char *topic) { char *tmp = (char *)realloc(g_session.mqtt_topic, strlen(topic) + 1); if (tmp == NULL) { + // keep the old value. realloc leaves it valid on failure, and dropping it + // would hand every later reader a NULL PrintAndLogEx(WARNING, "Failed to allocate memory"); - free(g_session.mqtt_topic); - g_session.mqtt_topic = NULL; return false; } g_session.mqtt_topic = tmp;