examples: Do not assume positive errno macros
authorTormod Volden <debian.tormod@gmail.com>
Sat, 22 Jan 2022 13:39:45 +0000 (14:39 +0100)
committerTormod Volden <debian.tormod@gmail.com>
Sat, 22 Jan 2022 13:47:17 +0000 (14:47 +0100)
Some functions were returning e.g. -ENOMEM and the caller would check
for negative return values. However, on Haiku, contrary to modern
standards, these macros are negative, so this logic would fail. In any
case, change the function to return -1 instead. For good measure also
set errno to the appropriate value, although it is not used in the
current code.

This was discovered on Haiku builds because the value for ENOMEM is
INT_MIN which cannot be negated without overflow.

Signed-off-by: Tormod Volden <debian.tormod@gmail.com>
examples/dpfp.c
examples/ezusb.c
examples/sam3u_benchmark.c
libusb/version_nano.h

index a3a76df..6828650 100644 (file)
@@ -217,7 +217,11 @@ static thread_return_t __stdcall poll_thread_main(void *arg)
 static int find_dpfp_device(void)
 {
        devh = libusb_open_device_with_vid_pid(NULL, 0x05ba, 0x000a);
-       return devh ? 0 : -ENODEV;
+       if (!devh) {
+               errno = ENODEV;
+               return -1;
+       }
+       return 0;
 }
 
 static int print_f0_data(void)
@@ -316,13 +320,16 @@ static int set_mode_async(unsigned char data)
        unsigned char *buf = malloc(LIBUSB_CONTROL_SETUP_SIZE + 1);
        struct libusb_transfer *transfer;
 
-       if (!buf)
-               return -ENOMEM;
+       if (!buf) {
+               errno = ENOMEM;
+               return -1;
+       }
 
        transfer = libusb_alloc_transfer(0);
        if (!transfer) {
                free(buf);
-               return -ENOMEM;
+               errno = ENOMEM;
+               return -1;
        }
 
        printf("async set mode %02x\n", data);
@@ -547,12 +554,16 @@ static int do_init(void)
 static int alloc_transfers(void)
 {
        img_transfer = libusb_alloc_transfer(0);
-       if (!img_transfer)
-               return -ENOMEM;
+       if (!img_transfer) {
+               errno = ENOMEM;
+               return -1;
+       }
 
        irq_transfer = libusb_alloc_transfer(0);
-       if (!irq_transfer)
-               return -ENOMEM;
+       if (!irq_transfer) {
+               errno = ENOMEM;
+               return -1;
+       }
 
        libusb_fill_bulk_transfer(img_transfer, devh, EP_DATA, imgbuf,
                sizeof(imgbuf), cb_img, NULL, 0);
index ec60b0e..4bed12a 100644 (file)
@@ -139,7 +139,11 @@ static int ezusb_write(libusb_device_handle *device, const char *label,
                else
                        logerror("%s ==> %d\n", label, status);
        }
-       return (status < 0) ? -EIO : 0;
+       if (status < 0) {
+               errno = EIO;
+               return -1;
+       }
+       return 0;
 }
 
 /*
@@ -162,7 +166,11 @@ static int ezusb_read(libusb_device_handle *device, const char *label,
                else
                        logerror("%s ==> %d\n", label, status);
        }
-       return (status < 0) ? -EIO : 0;
+       if (status < 0) {
+               errno = EIO;
+               return -1;
+       }
+       return 0;
 }
 
 /*
@@ -514,7 +522,8 @@ static int ram_poke(void *context, uint32_t addr, bool external,
                if (external) {
                        logerror("can't write %u bytes external memory at 0x%08x\n",
                                (unsigned)len, addr);
-                       return -EINVAL;
+                       errno = EINVAL;
+                       return -1;
                }
                break;
        case skip_internal:             /* CPU must be running */
@@ -538,7 +547,8 @@ static int ram_poke(void *context, uint32_t addr, bool external,
        case _undef:
        default:
                logerror("bug\n");
-               return -EDOM;
+               errno = EDOM;
+               return -1;
        }
 
        ctx->total += len;
index 33e8913..8979775 100644 (file)
@@ -122,8 +122,10 @@ static int benchmark_in(uint8_t ep)
                num_iso_pack = 16;
 
        xfr = libusb_alloc_transfer(num_iso_pack);
-       if (!xfr)
-               return -ENOMEM;
+       if (!xfr) {
+               errno = ENOMEM;
+               return -1;
+       }
 
        if (ep == EP_ISO_IN) {
                libusb_fill_iso_transfer(xfr, devh, ep, buf,
index 1261c1b..ae20d7d 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11689
+#define LIBUSB_NANO 11690