From 1292358d430b5b8c44552494a288ea59ab153d7c Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 25 Dec 2019 15:23:53 -0800 Subject: [PATCH] Don't just have a single general-purpose buffer in probe_devices(). Have separate buffers for device pathnames and USB device descriptors; that may take extra memory on the stack, but it makes the code somewhat cleaner. --- pcap-usb-linux.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pcap-usb-linux.c b/pcap-usb-linux.c index c495aa920f..d6e368ddb6 100644 --- a/pcap-usb-linux.c +++ b/pcap-usb-linux.c @@ -490,6 +490,8 @@ int usb_mmap(pcap_t* handle) #define USB_DT_DEVICE 1 +#define USB_DEVICE_DESCRIPTOR_SIZE 18 + /* probe the descriptors of the devices attached to the bus */ /* the descriptors will end up in the captured packet stream */ /* and be decoded by external apps like wireshark */ @@ -500,12 +502,13 @@ probe_devices(int bus) struct usbdevfs_ctrltransfer ctrl; struct dirent* data; int ret = 0; - char buf[sizeof("/dev/bus/usb/000/") + NAME_MAX]; + char busdevpath[sizeof("/dev/bus/usb/000/") + NAME_MAX]; DIR* dir; + char descriptor[USB_DEVICE_DESCRIPTOR_SIZE]; /* scan usb bus directories for device nodes */ - snprintf(buf, sizeof(buf), "/dev/bus/usb/%03d", bus); - dir = opendir(buf); + snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d", bus); + dir = opendir(busdevpath); if (!dir) return; @@ -516,9 +519,9 @@ probe_devices(int bus) if (name[0] == '.') continue; - snprintf(buf, sizeof(buf), "/dev/bus/usb/%03d/%s", bus, data->d_name); + snprintf(busdevpath, sizeof(busdevpath), "/dev/bus/usb/%03d/%s", bus, data->d_name); - fd = open(buf, O_RDWR); + fd = open(busdevpath, O_RDWR); if (fd == -1) continue; @@ -531,15 +534,15 @@ probe_devices(int bus) ctrl.bRequest = USB_REQ_GET_DESCRIPTOR; ctrl.wValue = USB_DT_DEVICE << 8; ctrl.wIndex = 0; - ctrl.wLength = sizeof(buf); + ctrl.wLength = sizeof(descriptor); #else ctrl.requesttype = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE; ctrl.request = USB_REQ_GET_DESCRIPTOR; ctrl.value = USB_DT_DEVICE << 8; ctrl.index = 0; - ctrl.length = sizeof(buf); + ctrl.length = sizeof(descriptor); #endif - ctrl.data = buf; + ctrl.data = descriptor; ctrl.timeout = CTRL_TIMEOUT; ret = ioctl(fd, USBDEVFS_CONTROL, &ctrl);