Skip to content

Commit

Permalink
device/client: make device_get_list() allocate the result buffer itself
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikias committed Jan 9, 2014
1 parent d04ce1b commit 678149c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
42 changes: 19 additions & 23 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,23 +267,21 @@ static int send_device_list(struct mux_client *client, uint32_t tag)
plist_t dict = plist_new_dict();
plist_t devices = plist_new_array();

int count = device_get_count(0);
if (count > 0) {
struct device_info *devs;
struct device_info *dev;
int i;

devs = malloc(sizeof(struct device_info) * count);
count = device_get_list(0, devs);
dev = devs;
for (i = 0; i < count; i++) {
plist_t device = create_device_attached_plist(dev++);
if (device) {
plist_array_append_item(devices, device);
}
struct device_info *devs = NULL;
struct device_info *dev;
int i;

int count = device_get_list(0, &devs);
dev = devs;
for (i = 0; devs && i < count; i++) {
plist_t device = create_device_attached_plist(dev++);
if (device) {
plist_array_append_item(devices, device);
}
free(devs);
}
if (devs)
free(devs);

plist_dict_insert_item(dict, "DeviceList", devices);
res = send_plist_pkt(client, tag, dict);
plist_free(dict);
Expand Down Expand Up @@ -369,25 +367,23 @@ static int notify_device_remove(struct mux_client *client, uint32_t device_id)

static int start_listen(struct mux_client *client)
{
struct device_info *devs;
struct device_info *devs = NULL;
struct device_info *dev;
int count, i;

client->state = CLIENT_LISTEN;
count = device_get_count(0);
if(!count)
return 0;
devs = malloc(sizeof(struct device_info) * count);
count = device_get_list(0, devs);

count = device_get_list(0, &devs);
dev = devs;
for(i=0; i<count; i++) {
for(i=0; devs && i < count; i++) {
if(notify_device_add(client, dev++) < 0) {
free(devs);
return -1;
}
}
free(devs);
if (devs)
free(devs);

return count;
}

Expand Down
7 changes: 6 additions & 1 deletion src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -746,10 +746,15 @@ int device_get_count(int include_hidden)
return count;
}

int device_get_list(int include_hidden, struct device_info *p)
int device_get_list(int include_hidden, struct device_info **devices)
{
int count = 0;
pthread_mutex_lock(&device_list_mutex);

int total_count = collection_count(&device_list);
*devices = malloc(sizeof(struct device_info) * total_count);
struct device_info *p = *devices;

FOREACH(struct mux_device *dev, &device_list) {
if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) {
p->id = dev->id;
Expand Down
2 changes: 1 addition & 1 deletion src/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void device_set_visible(int device_id);
void device_set_preflight_cb_data(int device_id, void* data);

int device_get_count(int include_hidden);
int device_get_list(int include_hidden, struct device_info *p);
int device_get_list(int include_hidden, struct device_info **devices);

int device_get_timeout(void);
void device_check_timeouts(void);
Expand Down

0 comments on commit 678149c

Please sign in to comment.