From a1e08f4b70a552d2ab2b8989a19da5aef83d0a7d Mon Sep 17 00:00:00 2001 From: tresf Date: Tue, 16 May 2023 14:27:51 -0400 Subject: [PATCH] Fix various NullPointerException fallthrough cases. Closes #1130 --- src/qz/communication/UsbIO.java | 23 ++++++++++++++++++----- src/qz/utils/UsbUtilities.java | 18 ++++++++++++++++-- 2 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/qz/communication/UsbIO.java b/src/qz/communication/UsbIO.java index 2d553a5ef..18a47c325 100644 --- a/src/qz/communication/UsbIO.java +++ b/src/qz/communication/UsbIO.java @@ -22,9 +22,15 @@ public UsbIO(DeviceOptions dOpts) throws DeviceException { if (dOpts.getInterfaceId() == null) { throw new IllegalArgumentException("Device interface cannot be null"); } - - this.device = device; this.iface = device.getActiveUsbConfiguration().getUsbInterface(dOpts.getInterfaceId()); + if (iface == null) { + throw new DeviceException(String.format("Could not find USB interface matching [ vendorId: '%s', productId: '%s', interface: '%s' ]", + "0x" + UsbUtil.toHexString(dOpts.getVendorId()), + "0x" + UsbUtil.toHexString(dOpts.getProductId()), + "0x" + UsbUtil.toHexString(dOpts.getInterfaceId()))); + } + this.device = device; + } public void open() throws DeviceException { @@ -100,19 +106,26 @@ public void sendFeatureReport(byte[] data, Byte reportId) throws DeviceException * @param endpoint Endpoint on the usb device interface to pass data across * @param data Byte array of data to send, or to be written from a receive */ - private synchronized void exchangeData(Byte endpoint, byte[] data) throws UsbException { + private synchronized void exchangeData(Byte endpoint, byte[] data) throws UsbException, DeviceException { if (endpoint == null) { throw new IllegalArgumentException("Interface endpoint cannot be null"); } - UsbPipe pipe = iface.getUsbEndpoint(endpoint).getUsbPipe(); + UsbEndpoint usbEndpoint = iface.getUsbEndpoint(endpoint); + if(usbEndpoint == null) { + throw new DeviceException(String.format("Could not find USB endpoint matching [ endpoint: '%s' ]", + "0x" + UsbUtil.toHexString(endpoint))); + } + UsbPipe pipe = usbEndpoint.getUsbPipe(); if (!pipe.isOpen()) { pipe.open(); } try { pipe.syncSubmit(data); } finally { - pipe.close(); + if(pipe != null) { + pipe.close(); + } } } diff --git a/src/qz/utils/UsbUtilities.java b/src/qz/utils/UsbUtilities.java index 8ac976bf1..02928665d 100644 --- a/src/qz/utils/UsbUtilities.java +++ b/src/qz/utils/UsbUtilities.java @@ -91,7 +91,13 @@ public static JSONArray getUsbDevicesJSON(boolean includeHubs) throws DeviceExce public static UsbDevice findDevice(Short vendorId, Short productId) throws DeviceException { try { - return findDevice(UsbHostManager.getUsbServices().getRootUsbHub(), vendorId, productId); + UsbDevice device = findDevice(UsbHostManager.getUsbServices().getRootUsbHub(), vendorId, productId); + if(device == null) { + throw new DeviceException(String.format("Could not find USB device matching [ vendorId: '%s', productId: '%s' ]", + "0x" + UsbUtil.toHexString(vendorId), + "0x" + UsbUtil.toHexString(productId))); + } + return device; } catch(UsbException e) { throw new DeviceException(e); @@ -148,7 +154,15 @@ public static List getInterfaceEndpoints(Short vendorId, Short productId, Byte i throw new IllegalArgumentException("Device interface cannot be null"); } - return findDevice(vendorId, productId).getActiveUsbConfiguration().getUsbInterface(iface).getUsbEndpoints(); + UsbInterface usbInterface = findDevice(vendorId, productId).getActiveUsbConfiguration().getUsbInterface(iface); + if(usbInterface != null) { + return usbInterface.getUsbEndpoints(); + } + throw new DeviceException(String.format("Could not find USB interface matching [ vendorId: '%s', productId: '%s', interface: '%s' ]", + "0x" + UsbUtil.toHexString(vendorId), + "0x" + UsbUtil.toHexString(productId), + "0x" + UsbUtil.toHexString(iface))); + } public static JSONArray getInterfaceEndpointsJSON(DeviceOptions dOpts) throws DeviceException {