]> git.ipfire.org Git - thirdparty/collectd.git/commitdiff
MQTT: fix off-by-one error in published message length (#4005)
authorLeonard Göhrs <leonard@goehrs.eu>
Wed, 1 Jun 2022 06:04:28 +0000 (08:04 +0200)
committerGitHub <noreply@github.com>
Wed, 1 Jun 2022 06:04:28 +0000 (08:04 +0200)
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 <l.goehrs@pengutronix.de>
src/mqtt.c

index 01c2ea26340977b26e02182f6bb76c9cc52cad22..26e31d66c6448ba7aab46b4dbf25fe4ceb75dd86 100644 (file)
@@ -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;