Handle a negative number of bytes as an error

There is no reason for libusb to ever return a negative number of bytes,
but due to the use of a signed integer, it's technically possible. And
because libdivecomputer returns an unsigned integer, such a negative
number would be reported as a very large size.
This commit is contained in:
Jef Driesen 2020-07-17 20:52:48 +02:00
parent 33a20bd2b8
commit c77551b366
2 changed files with 12 additions and 4 deletions

View File

@ -513,10 +513,12 @@ dc_usb_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual)
int nbytes = 0;
int rc = libusb_bulk_transfer (usb->handle, usb->endpoint_in, data, size, &nbytes, usb->timeout);
if (rc != LIBUSB_SUCCESS) {
if (rc != LIBUSB_SUCCESS || nbytes < 0) {
ERROR (abstract->context, "Usb read bulk transfer failed (%s).",
libusb_error_name (rc));
status = syserror (rc);
if (nbytes < 0)
nbytes = 0;
goto out;
}
@ -535,10 +537,12 @@ dc_usb_write (dc_iostream_t *abstract, const void *data, size_t size, size_t *ac
int nbytes = 0;
int rc = libusb_bulk_transfer (usb->handle, usb->endpoint_out, (void *) data, size, &nbytes, 0);
if (rc != LIBUSB_SUCCESS) {
if (rc != LIBUSB_SUCCESS || nbytes < 0) {
ERROR (abstract->context, "Usb write bulk transfer failed (%s).",
libusb_error_name (rc));
status = syserror (rc);
if (nbytes < 0)
nbytes = 0;
goto out;
}

View File

@ -703,10 +703,12 @@ dc_usbhid_read (dc_iostream_t *abstract, void *data, size_t size, size_t *actual
#if defined(USE_LIBUSB)
int rc = libusb_interrupt_transfer (usbhid->handle, usbhid->endpoint_in, data, size, &nbytes, usbhid->timeout);
if (rc != LIBUSB_SUCCESS) {
if (rc != LIBUSB_SUCCESS || nbytes < 0) {
ERROR (abstract->context, "Usb read interrupt transfer failed (%s).",
libusb_error_name (rc));
status = syserror (rc);
if (nbytes < 0)
nbytes = 0;
goto out;
}
#elif defined(USE_HIDAPI)
@ -749,10 +751,12 @@ dc_usbhid_write (dc_iostream_t *abstract, const void *data, size_t size, size_t
}
int rc = libusb_interrupt_transfer (usbhid->handle, usbhid->endpoint_out, (void *) buffer, length, &nbytes, 0);
if (rc != LIBUSB_SUCCESS) {
if (rc != LIBUSB_SUCCESS || nbytes < 0) {
ERROR (abstract->context, "Usb write interrupt transfer failed (%s).",
libusb_error_name (rc));
status = syserror (rc);
if (nbytes < 0)
nbytes = 0;
goto out;
}