From 42ac121bd1a39af34db4e187967756c631b606c5 Mon Sep 17 00:00:00 2001 From: Nicholas Humfrey Date: Thu, 26 Nov 2015 20:29:30 +0000 Subject: [PATCH] Allow MQTT payloads without NULL string termination --- src/mqtt.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/mqtt.c b/src/mqtt.c index 1b71d423..2eb11520 100644 --- a/src/mqtt.c +++ b/src/mqtt.c @@ -176,9 +176,10 @@ static void on_message ( char *payload; int status; - if ((msg->payloadlen <= 0) - || (((uint8_t *) msg->payload)[msg->payloadlen - 1] != 0)) + if (msg->payloadlen <= 0) { + DEBUG ("mqtt plugin: message has empty payload"); return; + } topic = strdup (msg->topic); name = strip_prefix (topic); @@ -207,7 +208,15 @@ static void on_message ( } vl.values_len = ds->ds_num; - payload = strdup ((void *) msg->payload); + payload = malloc (msg->payloadlen+1); + if (payload == NULL) + { + ERROR ("mqtt plugin: malloc for payload buffer failed."); + return; + } + memmove (payload, msg->payload, msg->payloadlen); + payload[msg->payloadlen] = 0; + DEBUG ("mqtt plugin: payload = \"%s\"", payload); status = parse_values (payload, &vl, ds); if (status != 0) -- 2.11.0