From bc747dce0947011b5963462e70c5403022853a04 Mon Sep 17 00:00:00 2001 From: jarlamsa Date: Thu, 1 Apr 2021 13:22:07 +0300 Subject: [PATCH] Check for duplicate status callbacks before adding to the list Duplicate status callbacks don't bring any value and can cause memory leaks if caller calls for this function multiple times with same status_cb. Also remove all the instances of the status_cb when removing event listener. --- connectivity/netsocket/source/NetworkInterface.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/connectivity/netsocket/source/NetworkInterface.cpp b/connectivity/netsocket/source/NetworkInterface.cpp index 3dcd4b33fee..3b2932ff8fa 100644 --- a/connectivity/netsocket/source/NetworkInterface.cpp +++ b/connectivity/netsocket/source/NetworkInterface.cpp @@ -145,6 +145,13 @@ static void call_all_event_listeners(NetworkInterface *iface, nsapi_event_t even void NetworkInterface::add_event_listener(mbed::Callback status_cb) { iface_eventlist_t *event_list = get_interface_event_list_head(); +#if MBED_CONF_PLATFORM_CALLBACK_COMPARABLE + ns_list_foreach_safe(iface_eventlist_entry_t, entry, event_list) { + if (entry->status_cb == status_cb && entry->iface == this) { + return; + } + } +#endif iface_eventlist_entry_t *entry = new iface_eventlist_entry_t; entry->iface = this; entry->status_cb = status_cb; @@ -160,7 +167,6 @@ void NetworkInterface::remove_event_listener(mbed::Callbackstatus_cb == status_cb && entry->iface == this) { ns_list_remove(event_list, entry); delete entry; - return; } } }