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>
This commit is contained in:
Blue Swirl 2011-01-09 14:43:33 +00:00
parent d66bddd7a4
commit 1a20a032cc

View File

@ -306,14 +306,15 @@ USBDevice *usb_host_device_open(const char *devname)
{ {
struct usb_device_info bus_info, dev_info; struct usb_device_info bus_info, dev_info;
USBDevice *d = NULL; USBDevice *d = NULL;
USBHostDevice *dev; USBHostDevice *dev, *ret = NULL;
char ctlpath[PATH_MAX + 1]; char ctlpath[PATH_MAX + 1];
char buspath[PATH_MAX + 1]; char buspath[PATH_MAX + 1];
int bfd, dfd, bus, address, i; int bfd, dfd, bus, address, i;
int ugendebug = UGEN_DEBUG_LEVEL; int ugendebug = UGEN_DEBUG_LEVEL;
if (usb_host_find_device(&bus, &address, devname) < 0) if (usb_host_find_device(&bus, &address, devname) < 0) {
return NULL; goto fail;
}
snprintf(buspath, PATH_MAX, "/dev/usb%d", bus); 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", printf("usb_host_device_open: failed to open usb bus - %s\n",
strerror(errno)); strerror(errno));
#endif #endif
return NULL; goto fail;
} }
bus_info.udi_addr = address; 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", printf("usb_host_device_open: failed to grab bus information - %s\n",
strerror(errno)); strerror(errno));
#endif #endif
return NULL; goto fail_bfd;
} }
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
@ -350,46 +351,52 @@ USBDevice *usb_host_device_open(const char *devname)
ctlpath, strerror(errno)); ctlpath, strerror(errno));
#endif #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 #ifdef DEBUG
printf("usb_host_device_open: failed to grab device info - %s\n", printf("usb_host_device_open: failed to grab device info - %s\n",
strerror(errno)); strerror(errno));
#endif #endif
goto fail; goto fail_dfd;
} }
d = usb_create(NULL /* FIXME */, "usb-host"); d = usb_create(NULL /* FIXME */, "usb-host");
dev = DO_UPCAST(USBHostDevice, dev, d); dev = DO_UPCAST(USBHostDevice, dev, d);
if (dev_info.udi_speed == 1) if (dev_info.udi_speed == 1) {
dev->dev.speed = USB_SPEED_LOW - 1; dev->dev.speed = USB_SPEED_LOW - 1;
else } else {
dev->dev.speed = USB_SPEED_FULL - 1; dev->dev.speed = USB_SPEED_FULL - 1;
}
if (strncmp(dev_info.udi_product, "product", 7) != 0) if (strncmp(dev_info.udi_product, "product", 7) != 0) {
pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc), pstrcpy(dev->dev.product_desc, sizeof(dev->dev.product_desc),
dev_info.udi_product); dev_info.udi_product);
else } else {
snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc), snprintf(dev->dev.product_desc, sizeof(dev->dev.product_desc),
"host:%s", devname); "host:%s", devname);
}
pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/"); pstrcpy(dev->devpath, sizeof(dev->devpath), "/dev/");
pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]); pstrcat(dev->devpath, sizeof(dev->devpath), dev_info.udi_devnames[0]);
/* Mark the endpoints as not yet open */ /* Mark the endpoints as not yet open */
for (i = 0; i < USB_MAX_ENDPOINTS; i++) for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
dev->ep_fd[i] = -1; 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: fail:
return NULL; return ret;
} }
static struct USBDeviceInfo usb_host_dev_info = { static struct USBDeviceInfo usb_host_dev_info = {