diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 7dcb442da8..56d8d6d5d6 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -636,17 +636,12 @@ kernel_pid_t gcoap_init(void) void gcoap_register_listener(gcoap_listener_t *listener) { - /* Add the listener to the end of the linked list. */ - gcoap_listener_t *_last = _coap_state.listeners; - while (_last->next) { - _last = _last->next; - } - - listener->next = NULL; if (!listener->link_encoder) { listener->link_encoder = gcoap_encode_link; } - _last->next = listener; + + listener->next = _coap_state.listeners; + _coap_state.listeners = listener; } int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, @@ -875,8 +870,7 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf) { assert(cf == COAP_FORMAT_LINK); - /* skip the first listener, gcoap itself (we skip /.well-known/core) */ - gcoap_listener_t *listener = _coap_state.listeners->next; + gcoap_listener_t *listener = _coap_state.listeners; char *out = (char *)buf; size_t pos = 0; @@ -887,7 +881,7 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf) ctx.flags = COAP_LINK_FLAG_INIT_RESLIST; /* write payload */ - while (listener) { + for (; listener != NULL; listener = listener->next) { if (!listener->link_encoder) { continue; } @@ -912,8 +906,6 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf) break; } } - - listener = listener->next; } return (int)pos; diff --git a/tests/unittests/tests-gcoap/tests-gcoap.c b/tests/unittests/tests-gcoap/tests-gcoap.c index 7ba737868d..71c6464574 100644 --- a/tests/unittests/tests-gcoap/tests-gcoap.c +++ b/tests/unittests/tests-gcoap/tests-gcoap.c @@ -49,7 +49,7 @@ static gcoap_listener_t listener_second = { .next = NULL }; -static const char *resource_list_str = ",,,"; +static const char *resource_list_str = ",,,"; /* * Client GET request success case. Test request generation.