shared/util: use 64-bit bitmap in util_get/clear_uid
authorPauli Virtanen <pav@iki.fi>
Sun, 5 Sep 2021 15:43:55 +0000 (18:43 +0300)
committerAyush Garg <ayush.garg@samsung.com>
Fri, 11 Mar 2022 13:38:36 +0000 (19:08 +0530)
The util_get/clear_uid functions use int type for bitmap, and are used
e.g. for SEID allocation. However, valid SEIDs are in range 1 to 0x3E
(AVDTP spec v1.3, 8.20.1), and 8*sizeof(int) is often smaller than 0x3E.

The function is also used in src/advertising.c, but an explicit maximum
value is always provided, so growing the bitmap size is safe there.

Use 64-bit bitmap instead, to be able to cover the valid range.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
android/avdtp.c
profiles/audio/avdtp.c
src/advertising.c
src/shared/util.c
src/shared/util.h
unit/test-avdtp.c

index 8c2930e..a261a8e 100755 (executable)
@@ -34,7 +34,7 @@
 #include "../profiles/audio/a2dp-codecs.h"
 
 #define MAX_SEID 0x3E
-static unsigned int seids;
+static uint64_t seids;
 
 #ifndef MAX
 # define MAX(x, y) ((x) > (y) ? (x) : (y))
index c513237..ef4f66a 100644 (file)
@@ -57,7 +57,7 @@
 #define AVDTP_PSM 25
 
 #define MAX_SEID 0x3E
-static unsigned int seids;
+static uint64_t seids;
 
 #ifndef MAX
 # define MAX(x, y) ((x) > (y) ? (x) : (y))
index 35d61b4..e71d1af 100644 (file)
@@ -43,7 +43,7 @@ struct btd_adv_manager {
        uint8_t max_scan_rsp_len;
        uint8_t max_ads;
        uint32_t supported_flags;
-       unsigned int instance_bitmap;
+       uint64_t instance_bitmap;
        bool extended_add_cmds;
        int8_t min_tx_power;
        int8_t max_tx_power;
index 4fc40d7..a344af8 100755 (executable)
@@ -123,30 +123,28 @@ unsigned char util_get_dt(const char *parent, const char *name)
 
 /* Helpers for bitfield operations */
 
-/* Find unique id in range from 1 to max but no bigger then
- * sizeof(int) * 8. ffs() is used since it is POSIX standard
- */
-uint8_t util_get_uid(unsigned int *bitmap, uint8_t max)
+/* Find unique id in range from 1 to max but no bigger than 64. */
+uint8_t util_get_uid(uint64_t *bitmap, uint8_t max)
 {
        uint8_t id;
 
-       id = ffs(~*bitmap);
+       id = ffsll(~*bitmap);
 
        if (!id || id > max)
                return 0;
 
-       *bitmap |= 1u << (id - 1);
+       *bitmap |= ((uint64_t)1) << (id - 1);
 
        return id;
 }
 
 /* Clear id bit in bitmap */
-void util_clear_uid(unsigned int *bitmap, uint8_t id)
+void util_clear_uid(uint64_t *bitmap, uint8_t id)
 {
-       if (!id)
+       if (!id || id > 64)
                return;
 
-       *bitmap &= ~(1u << (id - 1));
+       *bitmap &= ~(((uint64_t)1) << (id - 1));
 }
 
 static const struct {
index 9920b7f..6090837 100755 (executable)
@@ -102,8 +102,8 @@ void util_hexdump(const char dir, const unsigned char *buf, size_t len,
 
 unsigned char util_get_dt(const char *parent, const char *name);
 
-uint8_t util_get_uid(unsigned int *bitmap, uint8_t max);
-void util_clear_uid(unsigned int *bitmap, uint8_t id);
+uint8_t util_get_uid(uint64_t *bitmap, uint8_t max);
+void util_clear_uid(uint64_t *bitmap, uint8_t id);
 
 const char *bt_uuid16_to_str(uint16_t uuid);
 const char *bt_uuid32_to_str(uint32_t uuid);
index f5340d6..4e8a68c 100755 (executable)
@@ -550,7 +550,7 @@ static void test_server_seid(gconstpointer data)
        struct avdtp_local_sep *sep;
        unsigned int i;
 
-       for (i = 0; i < sizeof(int) * 8; i++) {
+       for (i = 0; i < MAX_SEID; i++) {
                sep = avdtp_register_sep(context->lseps, AVDTP_SEP_TYPE_SINK,
                                                AVDTP_MEDIA_TYPE_AUDIO,
                                                0x00, TRUE, &sep_ind, NULL,