coap_resource.c: Do not hold the next subscriber across a notification

coap_notify_observers() walked the subscribers with LL_FOREACH_SAFE(),
which holds on to the next subscription while the current one is
notified. The request handler runs with the lock released, and a
notification that cannot be sent disconnects the session, which deletes
all of that session's subscriptions. When the next subscription was one
of those, the walk carried on with freed memory.

Mark the subscriptions to visit at the start of the walk and look up the
next marked one from the head of the list after each notification, so
nothing deleted during a notification is used afterwards.

Subscriptions added during the walk are not notified in that pass, as
before.

Add a unit test.
This commit is contained in:
Alejandro Pacheco authored and Jon Shallow committed 2026-10-17 12:43:11 +01:00
1 parent a8f89ced78
commit da858655d6
3 files changed
+108 -2

No files matched your search

+2
View File
@@ -65,6 +65,8 @@ struct coap_subscription_t {
uint8_t dirty; /**< set if the notification temporarily could not be
* sent (in that case, the resource's partially
* dirty flag is set too) */
uint8_t to_visit; /**< set while coap_notify_observers() has still to
* visit this subscription */
coap_cache_key_t *cache_key; /** cache_key to identify requester */
coap_pdu_t *pdu; /**< PDU to use for additional requests */
};