From 8ee4bb2586aa08c25568320e4aa2dcd7370f9554 Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Thu, 16 Nov 2017 14:18:20 +0100 Subject: [PATCH] pdu.c: fix len returned by coap_get_data() for empty payload John Carter has provided this (slightly edited) patch to set len to 0 for PDUs that do not contain a payload. This makes this function more robust for callers that do not care about the function result. This change fixes issue 120 [1]. [1] https://github.com/obgm/libcoap/issues/120 --- src/pdu.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/pdu.c b/src/pdu.c index 0e47bbfb..e91e0f93 100644 --- a/src/pdu.c +++ b/src/pdu.c @@ -324,9 +324,15 @@ coap_get_data(coap_pdu_t *pdu, size_t *len, uint8_t **data) { assert(len); assert(data); - *len = pdu->used_size - (pdu->data - pdu->token); *data = pdu->data; - return *data != NULL; + if(pdu->data == NULL) { + *len = 0; + return 0; + } + + *len = pdu->used_size - (pdu->data - pdu->token); + + return 1; } #ifndef SHORT_ERROR_RESPONSE