From 5b4e4f047bb868e2b43e5fb81cce5a240a4f73ce Mon Sep 17 00:00:00 2001 From: Roger Light Date: Mon, 12 Oct 2020 21:53:43 +0100 Subject: [PATCH] Fix mosquitto_sub JSON printing with empty payloads. --- client/sub_client_output.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/client/sub_client_output.c b/client/sub_client_output.c index b039a115..378f0193 100644 --- a/client/sub_client_output.c +++ b/client/sub_client_output.c @@ -313,7 +313,11 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p /* Payload */ if(escaped){ - tmp = cJSON_CreateString(message->payload); + if(message->payload){ + tmp = cJSON_CreateString(message->payload); + }else{ + tmp = cJSON_CreateNull(); + } if(tmp == NULL){ cJSON_Delete(root); return MOSQ_ERR_NOMEM; @@ -321,10 +325,18 @@ static int json_print(const struct mosquitto_message *message, const mosquitto_p cJSON_AddItemToObject(root, "payload", tmp); }else{ return_parse_end = NULL; - tmp = cJSON_ParseWithOpts(message->payload, &return_parse_end, true); - if(tmp == NULL || return_parse_end != message->payload + message->payloadlen){ - cJSON_Delete(root); - return MOSQ_ERR_INVAL; + if(message->payload){ + tmp = cJSON_ParseWithOpts(message->payload, &return_parse_end, true); + if(tmp == NULL || return_parse_end != message->payload + message->payloadlen){ + cJSON_Delete(root); + return MOSQ_ERR_INVAL; + } + }else{ + tmp = cJSON_CreateNull(); + if(tmp == NULL){ + cJSON_Delete(root); + return MOSQ_ERR_INVAL; + } } cJSON_AddItemToObject(root, "payload", tmp); }