fix empty prefix issue

When switchtopic is set to a different topic the prefix will be empty so its length will be zero. In the Shorten function the strncmp will give a zero result because the prefixlen is zero and will add '~' even though there is no prefix.
Adding prefixlen != 0 in the if condition fixes the issue. Also skip adding the prefix "~" on line 292 by checking if the prefix length is greater than zero.
This commit is contained in:
engrbm87 2019-01-06 20:26:42 +02:00 committed by GitHub
parent d3020223d7
commit 122ae1ee27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 2 additions and 2 deletions

View File

@ -147,7 +147,7 @@ static void Shorten(char** s, char *prefix)
{
size_t len = strlen(*s);
size_t prefixlen = strlen(prefix);
if (len > prefixlen && !strncmp(*s, prefix, prefixlen)) {
if (len > prefixlen && prefixlen != 0 && !strncmp(*s, prefix, prefixlen)) {
*s += prefixlen-1;
*s[0] = '~';
}
@ -289,7 +289,7 @@ void HAssAnnounceButtonSwitch(byte device, char* topic, byte present, byte key,
snprintf_P(mqtt_data, sizeof(mqtt_data), HASS_DISCOVER_DEVICE_INFO, mqtt_data,
unique_id, ESP.getChipId(),
Settings.friendlyname[0], ModuleName().c_str(), my_version, my_image, "Tasmota");
snprintf_P(mqtt_data, sizeof(mqtt_data), HASS_DISCOVER_TOPIC_PREFIX, mqtt_data, prefix);
if (strlen(prefix) > 0 ) snprintf_P(mqtt_data, sizeof(mqtt_data), HASS_DISCOVER_TOPIC_PREFIX, mqtt_data, prefix);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s}"), mqtt_data);
}
MqttPublish(stopic, true);