diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index 035db1782a..8da3b9e75c 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -1763,6 +1763,7 @@ static inline size_t coap_opt_put_uri_query(uint8_t *buf, uint16_t lastonum, * @param[out] buf buffer to write to * @param[in,out] lastonum number of previous option (for delta calculation), * or 0 if first option + * May be NULL, then previous option is assumed to be 0. * @param[in] uri ptr into a source URI, to the first character after * the authority component * diff --git a/sys/net/application_layer/nanocoap/nanocoap.c b/sys/net/application_layer/nanocoap/nanocoap.c index a058ae1232..ad5311507c 100644 --- a/sys/net/application_layer/nanocoap/nanocoap.c +++ b/sys/net/application_layer/nanocoap/nanocoap.c @@ -904,6 +904,7 @@ size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char * { size_t len; const char *query = strchr(uri, '?'); + uint16_t _lastonum = lastonum ? *lastonum : 0; if (query) { len = (query == uri) ? 0 : (query - uri - 1); @@ -911,16 +912,20 @@ size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char * len = strlen(uri); } - size_t bytes_out = coap_opt_put_string_with_len(buf, *lastonum, + size_t bytes_out = coap_opt_put_string_with_len(buf, _lastonum, COAP_OPT_URI_PATH, uri, len, '/'); if (query) { buf += bytes_out; bytes_out += coap_opt_put_uri_query(buf, COAP_OPT_URI_PATH, query + 1); - *lastonum = COAP_OPT_URI_QUERY; + _lastonum = COAP_OPT_URI_QUERY; } else if (bytes_out) { - *lastonum = COAP_OPT_URI_PATH; + _lastonum = COAP_OPT_URI_PATH; + } + + if (lastonum) { + *lastonum = _lastonum; } return bytes_out;