diff options
author | Blue Swirl <blauwirbel@gmail.com> | 2011-01-09 14:43:33 +0000 |
---|---|---|
committer | Blue Swirl <blauwirbel@gmail.com> | 2011-01-09 14:43:33 +0000 |
commit | 1a20a032ccbb5800bfdfc75accfcff2ac67f5bcb (patch) | |
tree | e9c97c3b5f7ed0c24e49aea9a1f33eab000dd82d | |
parent | d66bddd7a4535cca3fc9e98c3195a7411779693c (diff) |
usb-bsd: fix a file descriptor leak
Fix a file descriptor leak reported by cppcheck:
[/src/qemu/usb-bsd.c:392]: (error) Resource leak: bfd
[/src/qemu/usb-bsd.c:388]: (error) Resource leak: dfd
Rearrange the code to avoid descriptor leaks. Also add braces as
needed.
Signed-off-by: Blue Swirl <blauwirbel@gmail.com>
-rw-r--r-- | usb-bsd.c | 71 |
1 files changed, 39 insertions, 32 deletions
@@ -306,14 +306,15 @@ USBDevice *usb_host_device_open(const char *devname) { struct usb_device_info bus_info, dev_info; USBDevice *d = NULL; - USBHostDevice *dev; + USBHostDevice *dev, *ret = NULL; char ctlpath[PATH_MAX + 1]; char buspath[PATH_MAX + 1]; int bfd, dfd, bus, address, i; int ugendebug = UGEN_DEBUG_LEVEL; - if (usb_host_find_device(&bus, &address, devname) < 0) - return NULL; + if (usb_host_find_device(&bus, &address, devname) < 0) { + goto fail; + } snprintf(buspath, PATH_MAX, "/dev/usb%d", bus); @@ -323,7 +324,7 @@ USBDevice *usb_host_device_open(const char *devname) printf("usb_host_device_open: failed to open usb bus - %s\n", strerror(errno)); #endif - return NULL; + goto fail; } bus_info.udi_addr = address; @@ -332,7 +333,7 @@ USBDevice *usb_host_device_open(const char *devname) printf("usb_host_device_open: failed to grab bus information - %s\n", strerror(errno)); #endif - return NULL; + goto fail_bfd; } #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) @@ -350,46 +351,52 @@ USBDevice *usb_host_device_open(const char *devname) ctlpath, strerror(errno)); #endif } + goto fail_dfd; } - if (dfd >= 0) { - if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) { + if (ioctl(dfd, USB_GET_DEVICEINFO, &dev_info) < 0) { #ifdef DEBUG - printf("usb_host_device_open: failed to grab device info - %s\n", - strerror(errno)); + printf("usb_host_device_open: failed to grab device info - %s\n", + strerror(errno)); #endif - goto fail; - } + goto fail_dfd; + } - d = usb_create(NULL /* FIXME */, "usb-host"); - dev = DO_UPCAST(USBHostDevice, dev, d); + d = usb_create(NULL /* FIXME */, "usb-host"); + dev = DO_UPCAST(USBHostDevice, dev, d); - if (dev_info.udi_speed == 1) - dev->dev.speed = USB_SPEED_LOW - 1; - else - dev->dev.speed = USB_SPEED_FULL - 1; + if (dev_info.udi_speed == 1) { + dev->dev.speed = USB_SPEED_LOW - 1; + } else { + dev->dev.speed = USB_SPEED_FULL - 1; + } - if (strncmp(dev_info.udi_product, "product", 7) != 0) - pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), - dev_info.udi_product); - else - snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), - "host:%s", devname); + if (strncmp(dev_info.udi_product, "product", 7) != 0) { + pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), + dev_info.udi_product); + } else { + snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), + "host:%s", devname); + } - pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); - pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); + pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); + pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); - /* Mark the endpoints as not yet open */ - for (i = 0; i < USB_MAX_ENDPOINTS; i++) - dev->ep_fd[i] = -1; + /* Mark the endpoints as not yet open */ + for (i = 0; i < USB_MAX_ENDPOINTS; i++) { + dev->ep_fd[i] = -1; + } - ioctl(dfd, USB_SETDEBUG, &ugendebug); + ioctl(dfd, USB_SETDEBUG, &ugendebug); - return (USBDevice *)dev; - } + ret = (USBDevice *)dev; +fail_dfd: + close(dfd); +fail_bfd: + close(bfd); fail: - return NULL; + return ret; } static struct USBDeviceInfo usb_host_dev_info = { |