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
This commit is contained in:
Olaf Bergmann committed 2017-11-16 14:18:20 +01:00
1 parent 0c837cdc77
commit 8ee4bb2586
1 file changed
+8 -2
+8 -2
View File
@@ -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