crash if mqttBrokerAddress is null

This commit is contained in:
Shengliang Guan 2020-06-01 14:54:37 +08:00
parent d5db330398
commit 4be24ad543
1 changed files with 9 additions and 5 deletions

View File

@ -49,6 +49,11 @@ int32_t mqttInitSystem() {
recntStatus.user_name = strstr(url, "@") != NULL ? strbetween(url, "//", ":") : NULL; recntStatus.user_name = strstr(url, "@") != NULL ? strbetween(url, "//", ":") : NULL;
recntStatus.password = strstr(url, "@") != NULL ? strbetween(strstr(url, recntStatus.user_name), ":", "@") : NULL; recntStatus.password = strstr(url, "@") != NULL ? strbetween(strstr(url, recntStatus.user_name), ":", "@") : NULL;
if (strlen(url) == 0) {
mqttError("failed to initialize mqtt module, reason: url is null");
return rc;
}
if (strstr(url, "@") != NULL) { if (strstr(url, "@") != NULL) {
recntStatus.hostname = strbetween(url, "@", ":"); recntStatus.hostname = strbetween(url, "@", ":");
} else if (strstr(strstr(url, "://") + 3, ":") != NULL) { } else if (strstr(strstr(url, "://") + 3, ":") != NULL) {
@ -81,10 +86,9 @@ int32_t mqttStartSystem() {
if (recntStatus.user_name != NULL && recntStatus.password != NULL) { if (recntStatus.user_name != NULL && recntStatus.password != NULL) {
mqttPrint("connecting to mqtt://%s:%s@%s:%s/%s/", recntStatus.user_name, recntStatus.password, mqttPrint("connecting to mqtt://%s:%s@%s:%s/%s/", recntStatus.user_name, recntStatus.password,
recntStatus.hostname, recntStatus.port, topicPath); recntStatus.hostname, recntStatus.port, topicPath);
} } else if (recntStatus.user_name != NULL && recntStatus.password == NULL) {
else if (recntStatus.user_name != NULL && recntStatus.password == NULL) mqttPrint("connecting to mqtt://%s@%s:%s/%s/", recntStatus.user_name, recntStatus.hostname, recntStatus.port,
{ topicPath);
mqttPrint("connecting to mqtt://%s@%s:%s/%s/", recntStatus.user_name,recntStatus.hostname, recntStatus.port, topicPath);
} }
mqtt_init_reconnect(&mqttClient, mqttReconnectClient, &recntStatus, mqtt_PublishCallback); mqtt_init_reconnect(&mqttClient, mqttReconnectClient, &recntStatus, mqtt_PublishCallback);