diff options
| author | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-15 19:02:06 +0200 |
|---|---|---|
| committer | Oskari Timperi <oskari.timperi@iki.fi> | 2017-02-15 19:02:06 +0200 |
| commit | a2c431ec96f7a6ed7e5c41b7ae93dc086ae541f1 (patch) | |
| tree | f3eb6bbe15833a564bb4469ccd7c0a3c3a5f2f7b /src | |
| parent | 0741e7d5810c1bb999b417f97dd66f5ba4cdc6c2 (diff) | |
| download | mqtt-a2c431ec96f7a6ed7e5c41b7ae93dc086ae541f1.tar.gz mqtt-a2c431ec96f7a6ed7e5c41b7ae93dc086ae541f1.zip | |
NULL terminate received strings and message payload
Diffstat (limited to 'src')
| -rw-r--r-- | src/deserialize.c | 6 | ||||
| -rw-r--r-- | src/stream_mqtt.c | 5 |
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; |
