OSDN Git Service

core: Fix device_get_name() to always null-terminate what it returns
authorPetri Gynther <pgynther@google.com>
Wed, 19 Feb 2014 00:44:13 +0000 (16:44 -0800)
committerJohan Hedberg <johan.hedberg@intel.com>
Wed, 19 Feb 2014 04:14:17 +0000 (06:14 +0200)
Fix device_get_name() to null-terminate the name string it returns.
Modify all callers as needed.

plugins/wiimote.c
profiles/input/device.c
src/device.c

index c23607f..bd8820e 100644 (file)
@@ -90,7 +90,6 @@ static ssize_t wii_pincb(struct btd_adapter *adapter, struct btd_device *device,
        product = btd_device_get_product(device);
 
        device_get_name(device, name, sizeof(name));
-       name[sizeof(name) - 1] = 0;
 
        for (i = 0; i < G_N_ELEMENTS(wii_ids); ++i) {
                if (vendor == wii_ids[i][0] && product == wii_ids[i][1])
index 10c3d5c..d6e97c7 100644 (file)
@@ -822,7 +822,7 @@ static struct input_device *input_device_new(struct btd_service *service)
        idev->handle = rec->handle;
        idev->disable_sdp = is_device_sdp_disable(rec);
 
-       device_get_name(device, name, HCI_MAX_NAME_LENGTH);
+       device_get_name(device, name, sizeof(name));
        if (strlen(name) > 0)
                idev->name = g_strdup(name);
 
index 41a6fc8..eabbe0a 100644 (file)
@@ -2272,7 +2272,10 @@ void btd_device_device_set_name(struct btd_device *device, const char *name)
 
 void device_get_name(struct btd_device *device, char *name, size_t len)
 {
-       strncpy(name, device->name, len);
+       if (name != NULL && len > 0) {
+               strncpy(name, device->name, len - 1);
+               name[len - 1] = '\0';
+       }
 }
 
 bool device_name_known(struct btd_device *device)