Skip to content

Commit 306acf8

Browse files
committed
Fix 'discovered_devices' list (set NULL after free + mutex)
1 parent 3c9b0ea commit 306acf8

File tree

2 files changed

+24
-21
lines changed

2 files changed

+24
-21
lines changed

dbus/gattlib_adapter.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: BSD-3-Clause
33
*
4-
* Copyright (c) 2016-2022, Olivier Martin <[email protected]>
4+
* Copyright (c) 2016-2024, Olivier Martin <[email protected]>
55
*/
66

77
#include "gattlib_internal.h"
@@ -124,13 +124,14 @@ static void device_manager_on_device1_signal(const char* device1_path, struct ga
124124
}
125125

126126
// Check if the device is already part of the list
127+
g_mutex_lock(&gattlib_adapter->ble_scan.discovered_devices_mutex);
127128
GSList *item = g_slist_find_custom(gattlib_adapter->ble_scan.discovered_devices, address, (GCompareFunc)g_ascii_strcasecmp);
128-
129129
// First time this device is in the list
130130
if (item == NULL) {
131131
// Add the device to the list
132132
gattlib_adapter->ble_scan.discovered_devices = g_slist_append(gattlib_adapter->ble_scan.discovered_devices, g_strdup(address));
133133
}
134+
g_mutex_unlock(&gattlib_adapter->ble_scan.discovered_devices_mutex);
134135

135136
if ((item == NULL) || (gattlib_adapter->ble_scan.enabled_filters & GATTLIB_DISCOVER_FILTER_NOTIFY_CHANGE)) {
136137
#if defined(WITH_PYTHON)
@@ -219,9 +220,6 @@ static void* _ble_scan_loop(void* args) {
219220
// Ensure BLE device discovery is stopped
220221
gattlib_adapter_scan_disable(gattlib_adapter);
221222

222-
// Free discovered device list
223-
g_slist_foreach(gattlib_adapter->ble_scan.discovered_devices, (GFunc)g_free, NULL);
224-
g_slist_free(gattlib_adapter->ble_scan.discovered_devices);
225223
return 0;
226224
}
227225

@@ -354,27 +352,31 @@ int gattlib_adapter_scan_enable(void* adapter, gattlib_discovered_device_t disco
354352

355353
int gattlib_adapter_scan_disable(void* adapter) {
356354
struct gattlib_adapter *gattlib_adapter = adapter;
355+
GError *error = NULL;
357356

358-
if (gattlib_adapter->ble_scan.scan_loop) {
359-
GError *error = NULL;
357+
if (gattlib_adapter->ble_scan.is_scanning) {
358+
g_mutex_lock(&gattlib_adapter->ble_scan.scan_loop_mutex);
359+
gattlib_adapter->ble_scan.is_scanning = false;
360+
g_cond_broadcast(&gattlib_adapter->ble_scan.scan_loop_cond);
361+
g_mutex_unlock(&gattlib_adapter->ble_scan.scan_loop_mutex);
362+
}
360363

361-
org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
362-
// Ignore the error
364+
org_bluez_adapter1_call_stop_discovery_sync(gattlib_adapter->adapter_proxy, NULL, &error);
365+
// Ignore the error
363366

364-
// Remove timeout
365-
if (gattlib_adapter->ble_scan.ble_scan_timeout_id) {
366-
g_source_remove(gattlib_adapter->ble_scan.ble_scan_timeout_id);
367-
gattlib_adapter->ble_scan.ble_scan_timeout_id = 0;
368-
}
369-
370-
// Ensure the scan loop is quit
371-
if (g_main_loop_is_running(gattlib_adapter->ble_scan.scan_loop)) {
372-
g_main_loop_quit(gattlib_adapter->ble_scan.scan_loop);
373-
}
374-
g_main_loop_unref(gattlib_adapter->ble_scan.scan_loop);
375-
gattlib_adapter->ble_scan.scan_loop = NULL;
367+
// Remove timeout
368+
if (gattlib_adapter->ble_scan.ble_scan_timeout_id) {
369+
g_source_remove(gattlib_adapter->ble_scan.ble_scan_timeout_id);
370+
gattlib_adapter->ble_scan.ble_scan_timeout_id = 0;
376371
}
377372

373+
// Free discovered device list
374+
g_mutex_lock(&gattlib_adapter->ble_scan.discovered_devices_mutex);
375+
g_slist_foreach(gattlib_adapter->ble_scan.discovered_devices, (GFunc)g_free, NULL);
376+
g_slist_free(gattlib_adapter->ble_scan.discovered_devices);
377+
gattlib_adapter->ble_scan.discovered_devices = NULL;
378+
g_mutex_unlock(&gattlib_adapter->ble_scan.discovered_devices_mutex);
379+
378380
return GATTLIB_SUCCESS;
379381
}
380382

dbus/gattlib_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ struct gattlib_adapter {
6767
// This list is used to stored discovered devices during BLE scan.
6868
// The list is freed when the BLE scanning is completed.
6969
GSList *discovered_devices;
70+
GMutex discovered_devices_mutex;
7071

7172
int added_signal_id;
7273
int changed_signal_id;

0 commit comments

Comments
 (0)