diff options
| author | 2014-01-09 11:31:49 +0100 | |
|---|---|---|
| committer | 2014-01-09 11:31:49 +0100 | |
| commit | 678149cde792d30beca94ba6fc9ea20996f2febc (patch) | |
| tree | d4427b08b3a900bb0e2e82921f98dae44f1f3017 | |
| parent | d04ce1b524f68dda6b75cfff69f70f4b4ad8e1d5 (diff) | |
| download | usbmuxd-678149cde792d30beca94ba6fc9ea20996f2febc.tar.gz usbmuxd-678149cde792d30beca94ba6fc9ea20996f2febc.tar.bz2 | |
device/client: make device_get_list() allocate the result buffer itself
Using device_get_count() and device_get_list() separately can return different
device counts in case there are devices added to the list inbetween these two
function calls. To prevent this, device_get_list() will allocate the buffer by
itself.
| -rw-r--r-- | src/client.c | 42 | ||||
| -rw-r--r-- | src/device.c | 7 | ||||
| -rw-r--r-- | src/device.h | 2 |
3 files changed, 26 insertions, 25 deletions
diff --git a/src/client.c b/src/client.c index d4719c3..5a70edb 100644 --- a/src/client.c +++ b/src/client.c | |||
| @@ -267,23 +267,21 @@ static int send_device_list(struct mux_client *client, uint32_t tag) | |||
| 267 | plist_t dict = plist_new_dict(); | 267 | plist_t dict = plist_new_dict(); |
| 268 | plist_t devices = plist_new_array(); | 268 | plist_t devices = plist_new_array(); |
| 269 | 269 | ||
| 270 | int count = device_get_count(0); | 270 | struct device_info *devs = NULL; |
| 271 | if (count > 0) { | 271 | struct device_info *dev; |
| 272 | struct device_info *devs; | 272 | int i; |
| 273 | struct device_info *dev; | 273 | |
| 274 | int i; | 274 | int count = device_get_list(0, &devs); |
| 275 | 275 | dev = devs; | |
| 276 | devs = malloc(sizeof(struct device_info) * count); | 276 | for (i = 0; devs && i < count; i++) { |
| 277 | count = device_get_list(0, devs); | 277 | plist_t device = create_device_attached_plist(dev++); |
| 278 | dev = devs; | 278 | if (device) { |
| 279 | for (i = 0; i < count; i++) { | 279 | plist_array_append_item(devices, device); |
| 280 | plist_t device = create_device_attached_plist(dev++); | ||
| 281 | if (device) { | ||
| 282 | plist_array_append_item(devices, device); | ||
| 283 | } | ||
| 284 | } | 280 | } |
| 285 | free(devs); | ||
| 286 | } | 281 | } |
| 282 | if (devs) | ||
| 283 | free(devs); | ||
| 284 | |||
| 287 | plist_dict_insert_item(dict, "DeviceList", devices); | 285 | plist_dict_insert_item(dict, "DeviceList", devices); |
| 288 | res = send_plist_pkt(client, tag, dict); | 286 | res = send_plist_pkt(client, tag, dict); |
| 289 | plist_free(dict); | 287 | plist_free(dict); |
| @@ -369,25 +367,23 @@ static int notify_device_remove(struct mux_client *client, uint32_t device_id) | |||
| 369 | 367 | ||
| 370 | static int start_listen(struct mux_client *client) | 368 | static int start_listen(struct mux_client *client) |
| 371 | { | 369 | { |
| 372 | struct device_info *devs; | 370 | struct device_info *devs = NULL; |
| 373 | struct device_info *dev; | 371 | struct device_info *dev; |
| 374 | int count, i; | 372 | int count, i; |
| 375 | 373 | ||
| 376 | client->state = CLIENT_LISTEN; | 374 | client->state = CLIENT_LISTEN; |
| 377 | count = device_get_count(0); | ||
| 378 | if(!count) | ||
| 379 | return 0; | ||
| 380 | devs = malloc(sizeof(struct device_info) * count); | ||
| 381 | count = device_get_list(0, devs); | ||
| 382 | 375 | ||
| 376 | count = device_get_list(0, &devs); | ||
| 383 | dev = devs; | 377 | dev = devs; |
| 384 | for(i=0; i<count; i++) { | 378 | for(i=0; devs && i < count; i++) { |
| 385 | if(notify_device_add(client, dev++) < 0) { | 379 | if(notify_device_add(client, dev++) < 0) { |
| 386 | free(devs); | 380 | free(devs); |
| 387 | return -1; | 381 | return -1; |
| 388 | } | 382 | } |
| 389 | } | 383 | } |
| 390 | free(devs); | 384 | if (devs) |
| 385 | free(devs); | ||
| 386 | |||
| 391 | return count; | 387 | return count; |
| 392 | } | 388 | } |
| 393 | 389 | ||
diff --git a/src/device.c b/src/device.c index 29be9d1..0844499 100644 --- a/src/device.c +++ b/src/device.c | |||
| @@ -746,10 +746,15 @@ int device_get_count(int include_hidden) | |||
| 746 | return count; | 746 | return count; |
| 747 | } | 747 | } |
| 748 | 748 | ||
| 749 | int device_get_list(int include_hidden, struct device_info *p) | 749 | int device_get_list(int include_hidden, struct device_info **devices) |
| 750 | { | 750 | { |
| 751 | int count = 0; | 751 | int count = 0; |
| 752 | pthread_mutex_lock(&device_list_mutex); | 752 | pthread_mutex_lock(&device_list_mutex); |
| 753 | |||
| 754 | int total_count = collection_count(&device_list); | ||
| 755 | *devices = malloc(sizeof(struct device_info) * total_count); | ||
| 756 | struct device_info *p = *devices; | ||
| 757 | |||
| 753 | FOREACH(struct mux_device *dev, &device_list) { | 758 | FOREACH(struct mux_device *dev, &device_list) { |
| 754 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) { | 759 | if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) { |
| 755 | p->id = dev->id; | 760 | p->id = dev->id; |
diff --git a/src/device.h b/src/device.h index 95d470e..cb5bc24 100644 --- a/src/device.h +++ b/src/device.h | |||
| @@ -44,7 +44,7 @@ void device_set_visible(int device_id); | |||
| 44 | void device_set_preflight_cb_data(int device_id, void* data); | 44 | void device_set_preflight_cb_data(int device_id, void* data); |
| 45 | 45 | ||
| 46 | int device_get_count(int include_hidden); | 46 | int device_get_count(int include_hidden); |
| 47 | int device_get_list(int include_hidden, struct device_info *p); | 47 | int device_get_list(int include_hidden, struct device_info **devices); |
| 48 | 48 | ||
| 49 | int device_get_timeout(void); | 49 | int device_get_timeout(void); |
| 50 | void device_check_timeouts(void); | 50 | void device_check_timeouts(void); |
