diff --git a/dns_sd.c b/dns_sd.c index b739ab580..b5ce221ca 100644 --- a/dns_sd.c +++ b/dns_sd.c @@ -235,8 +235,8 @@ void remove_dup_discovery_data(struct dns_sd_discovery_data **ddata) int dnssd_context_scan(struct iio_scan_result *scan_result) { - struct iio_context_info **info; struct dns_sd_discovery_data *ddata, *ndata; + struct iio_context_info *info; int ret = 0; ret = dnssd_find_hosts(&ddata); @@ -251,14 +251,14 @@ int dnssd_context_scan(struct iio_scan_result *scan_result) goto fail; for (ndata = ddata; ndata->next != NULL; ndata = ndata->next) { - info = iio_scan_result_add(scan_result, 1); + info = iio_scan_result_add(scan_result); if (!info) { IIO_ERROR("Out of memory when adding new scan result\n"); ret = -ENOMEM; break; } - ret = dnssd_fill_context_info(*info, + ret = dnssd_fill_context_info(info, ndata->hostname, ndata->addr_str,ndata->port); if (ret < 0) { IIO_DEBUG("Failed to add %s (%s) err: %d\n", ndata->hostname, ndata->addr_str, ret); diff --git a/iio-private.h b/iio-private.h index 5ce458688..b5bbf93f3 100644 --- a/iio-private.h +++ b/iio-private.h @@ -209,8 +209,8 @@ struct iio_scan_result { struct iio_context_info **info; }; -struct iio_context_info ** iio_scan_result_add( - struct iio_scan_result *scan_result, size_t num); +struct iio_context_info * +iio_scan_result_add(struct iio_scan_result *scan_result); void free_channel(struct iio_channel *chn); void free_device(struct iio_device *dev); diff --git a/local.c b/local.c index 88a878eaa..e3a799de2 100644 --- a/local.c +++ b/local.c @@ -2132,7 +2132,7 @@ static int check_device(void *d, const char *path) int local_context_scan(struct iio_scan_result *scan_result) { - struct iio_context_info **info; + struct iio_context_info *info; bool exists = false; char *desc, *uri, *machine, buf[2 * BUF_SIZE], names[BUF_SIZE]; int ret; @@ -2169,12 +2169,13 @@ int local_context_scan(struct iio_scan_result *scan_result) if (!uri) goto err_free_desc; - info = iio_scan_result_add(scan_result, 1); + info = iio_scan_result_add(scan_result); if (!info) goto err_free_uri; - info[0]->description = desc; - info[0]->uri = uri; + info->description = desc; + info->uri = uri; + return 0; err_free_uri: diff --git a/scan.c b/scan.c index 925c7f103..4e3515801 100644 --- a/scan.c +++ b/scan.c @@ -86,33 +86,27 @@ void iio_context_info_list_free(struct iio_context_info **list) free(list); } -struct iio_context_info ** iio_scan_result_add( - struct iio_scan_result *scan_result, size_t num) +struct iio_context_info * +iio_scan_result_add(struct iio_scan_result *scan_result) { struct iio_context_info **info; - size_t old_size, new_size; - size_t i; + size_t size = scan_result->size; - old_size = scan_result->size; - new_size = old_size + num; - - info = realloc(scan_result->info, (new_size + 1) * sizeof(*info)); + info = realloc(scan_result->info, (size + 2) * sizeof(*info)); if (!info) return NULL; scan_result->info = info; - scan_result->size = new_size; + scan_result->size = size + 1; - for (i = old_size; i < new_size; i++) { - /* Make sure iio_context_info_list_free won't overflow */ - info[i + 1] = NULL; + /* Make sure iio_context_info_list_free won't overflow */ + info[size + 1] = NULL; - info[i] = zalloc(sizeof(**info)); - if (!info[i]) - return NULL; - } + info[size] = zalloc(sizeof(**info)); + if (!info[size]) + return NULL; - return &info[old_size]; + return info[size]; } struct iio_scan_context * iio_create_scan_context( diff --git a/usb.c b/usb.c index 8a7210d8b..c2645ca14 100644 --- a/usb.c +++ b/usb.c @@ -1202,7 +1202,7 @@ static int usb_fill_context_info(struct iio_context_info *info, int usb_context_scan(struct iio_scan_result *scan_result) { - struct iio_context_info **info; + struct iio_context_info *info; libusb_device **device_list; libusb_context *ctx; unsigned int i; @@ -1228,12 +1228,12 @@ int usb_context_scan(struct iio_scan_result *scan_result) continue; if (!iio_usb_match_device(dev, hdl, &intrfc)) { - info = iio_scan_result_add(scan_result, 1); + info = iio_scan_result_add(scan_result); if (!info) ret = -ENOMEM; else - ret = usb_fill_context_info(*info, dev, hdl, - intrfc); + ret = usb_fill_context_info(info, dev, hdl, + intrfc); } libusb_close(hdl);