Skip to content

Commit

Permalink
Fix various NullPointerException fallthrough cases.
Browse files Browse the repository at this point in the history
Closes qzind#1130
  • Loading branch information
tresf committed May 16, 2023
1 parent c26b7db commit a1e08f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
23 changes: 18 additions & 5 deletions src/qz/communication/UsbIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
}
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/qz/utils/UsbUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit a1e08f4

Please sign in to comment.