From: Leonard Göhrs Date: Wed, 1 Jun 2022 06:04:28 +0000 (+0200) Subject: MQTT: fix off-by-one error in published message length (#4005) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cecc91f1eec7f473acd19c49f256297cfb56003c;p=thirdparty%2Fcollectd.git MQTT: fix off-by-one error in published message length (#4005) The mqtt plugin publishes messages including the trailing '\0'-Byte, as can be verified using e.g. the mosquitto_sub command with a HEX message formatter: $ mosquitto_sub -t "#" -F "%t: %X" metrics/loragw1/users/users: 313635323334303737392E3938353A3000 ^^ While the the MQTT PUBLISH payload is, according to the specification, application specific and most (C-Based) consumers will not notice the trailing '\0'-Byte, it is rather uncommon to publish messages like this. We stumbled upon this error while using Telegraf to ingest metrics via MQTT, as it is Go-Based and does not use '\0'-terminated strings, leading to issues parsing these strings into numbers. Fix the off-by-one error by using the result of strlen() as-is. Signed-off-by: Leonard Göhrs --- diff --git a/src/mqtt.c b/src/mqtt.c index 01c2ea263..26e31d66c 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -532,7 +532,7 @@ static int mqtt_write(const data_set_t *ds, const value_list_t *vl, return status; } - status = publish(conf, topic, payload, strlen(payload) + 1); + status = publish(conf, topic, payload, strlen(payload)); if (status != 0) { ERROR("mqtt plugin: publish failed: %s", mosquitto_strerror(status)); return status;