shared/gatt-db: Fix gatt_db_attribute_get_index
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Fri, 1 Apr 2022 21:32:53 +0000 (14:32 -0700)
committerAyush Garg <ayush.garg@samsung.com>
Mon, 15 May 2023 09:25:54 +0000 (14:55 +0530)
gatt_db_attribute_get_index was calculating the index based on
attrib->handle - service->attributes[0]->handle which doesn't work when
there are gaps in between handles.

Fixes: https://github.com/bluez/bluez/issues/326
Signed-off-by: Manika Shrivastava <manika.sh@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
src/shared/gatt-db.c

index d85ed56..9d53708 100644 (file)
@@ -1544,12 +1544,12 @@ static int gatt_db_attribute_get_index(struct gatt_db_attribute *attrib)
                return -1;
 
        service = attrib->service;
-       index = attrib->handle - service->attributes[0]->handle;
-
-       if (index > (service->num_handles - 1))
-               return -1;
+       for (index = 0; index < service->num_handles; index++) {
+               if (service->attributes[index] == attrib)
+                       return index;
+       }
 
-       return index;
+       return -1;
 }
 
 static struct gatt_db_attribute *