Skip to content

Commit 0fa04a3

Browse files
committed
nanocoap: prevent integer underflow in coap_opt_put_uri_pathquery()
If uri contains no path but only a query "?foo=bar" `len` would underflow. Fix this by detecting if there is no path. Reported by @Yu3H0
1 parent 3a4ec8d commit 0fa04a3

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

sys/net/application_layer/nanocoap/nanocoap.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,8 +902,15 @@ size_t coap_opt_put_string_with_len(uint8_t *buf, uint16_t lastonum, uint16_t op
902902

903903
size_t coap_opt_put_uri_pathquery(uint8_t *buf, uint16_t *lastonum, const char *uri)
904904
{
905+
size_t len;
905906
const char *query = strchr(uri, '?');
906-
size_t len = query ? (size_t)(query - uri - 1) : strlen(uri);
907+
908+
if (query) {
909+
len = (query == uri) ? 0 : (query - uri - 1);
910+
} else {
911+
len = strlen(uri);
912+
}
913+
907914
size_t bytes_out = coap_opt_put_string_with_len(buf, *lastonum,
908915
COAP_OPT_URI_PATH,
909916
uri, len, '/');

0 commit comments

Comments
 (0)