aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/deserialize.c6
-rw-r--r--src/stream_mqtt.c5
2 files changed, 9 insertions, 2 deletions
diff --git a/src/deserialize.c b/src/deserialize.c
index 7f3da7c..aaff490 100644
--- a/src/deserialize.c
+++ b/src/deserialize.c
@@ -109,12 +109,16 @@ static int MqttPacketPublishDeserialize(MqttPacketPublish **packet, Stream *stre
LOG_DEBUG("reading payload payloadSize:%lu\n", payloadSize);
- (*packet)->message = bfromcstralloc(payloadSize, "");
+ /* Allocate extra byte for a NULL terminator. If the user tries to print
+ the payload directly. */
+
+ (*packet)->message = bfromcstralloc(payloadSize+1, "");
if (StreamRead(bdata((*packet)->message), payloadSize, stream) == -1)
return -1;
(*packet)->message->slen = payloadSize;
+ (*packet)->message->data[payloadSize] = '\0';
return 0;
}
diff --git a/src/stream_mqtt.c b/src/stream_mqtt.c
index c9d0e12..3864ef3 100644
--- a/src/stream_mqtt.c
+++ b/src/stream_mqtt.c
@@ -10,7 +10,9 @@ int64_t StreamReadMqttString(bstring *buf, Stream *stream)
if (StreamReadUint16Be(&len, stream) == -1)
return -1;
- result = bfromcstralloc(len, "");
+ /* We need 1 extra byte for a NULL terminator. bfromcstralloc doesn't do
+ any size snapping. */
+ result = bfromcstralloc(len+1, "");
if (!result)
return -1;
@@ -22,6 +24,7 @@ int64_t StreamReadMqttString(bstring *buf, Stream *stream)
}
result->slen = len;
+ result->data[len] = '\0';
*buf = result;