This now fixes all the warnings generated by Clang during the compilation of core.

The last remaining warning was due to an actual bug in libusb_get_next_timeout() where 0 is supposed to be returned on error, but instead the nonzero LIBUSB_ERROR_OTHER was. As a test for non-zero return was used for an indication of success, Clang properly flagged it as an issue.

Regards,

/Pete
>From 21ce67310216dd1173a582b584399bf6096965ea Mon Sep 17 00:00:00 2001
From: Pete Batard <p...@akeo.ie>
Date: Wed, 13 Jun 2012 13:33:00 +0100
Subject: [PATCH 1/2] Core: Fix Clang warnings

core.c:
* Result of 'malloc' is converted to a pointer of type 'struct libusb_device *',
  which is incompatible with sizeof operand type 'void *'
* Memory is never released; potential leak of memory pointed to by 'devs'
* Assigned value is garbage or undefined (due to potentially empty and
  uninitialized device list)
descriptor.c:
* Function call argument is an uninitialized value
io.c:
* Call to 'malloc' has an allocation size of 0 bytes
* Branch condition evaluates to a garbage value (due to get_next_timeout
  returning a negative error code instead of zero on error)
---
 libusb/core.c         |    3 ++-
 libusb/descriptor.c   |    4 +++-
 libusb/io.c           |    7 ++++---
 3 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/libusb/core.c b/libusb/core.c
index b0fa1c0..1e3edc6 100644
--- a/libusb/core.c
+++ b/libusb/core.c
@@ -609,7 +609,7 @@ ssize_t API_EXPORTED libusb_get_device_list(libusb_context 
*ctx,
 
        /* convert discovered_devs into a list */
        len = discdevs->len;
-       ret = malloc(sizeof(void *) * (len + 1));
+       ret = calloc(len + 1, sizeof(struct libusb_device *));
        if (!ret) {
                len = LIBUSB_ERROR_NO_MEM;
                goto out;
@@ -697,6 +697,7 @@ int API_EXPORTED libusb_get_port_path(libusb_context *ctx, 
libusb_device *dev, u
                        break;
                i--;
                if (i < 0) {
+                       libusb_free_device_list(devs, 1);
                        return LIBUSB_ERROR_OVERFLOW;
                }
                path[i] = dev->port_number;
diff --git a/libusb/descriptor.c b/libusb/descriptor.c
index 763f93c..0c5f51f 100644
--- a/libusb/descriptor.c
+++ b/libusb/descriptor.c
@@ -486,8 +486,10 @@ int API_EXPORTED 
libusb_get_active_config_descriptor(libusb_device *dev,
        if (r < 0)
                goto err;
 
+       _config->wTotalLength = 0;
        usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
-       buf = malloc(_config->wTotalLength);
+       if (_config->wTotalLength != 0)
+               buf = malloc(_config->wTotalLength);
        if (!buf) {
                r = LIBUSB_ERROR_NO_MEM;
                goto err;
diff --git a/libusb/io.c b/libusb/io.c
index e81ca8b..d06d375 100644
--- a/libusb/io.c
+++ b/libusb/io.c
@@ -1857,7 +1857,7 @@ static int handle_events(struct libusb_context *ctx, 
struct timeval *tv)
        int r;
        struct usbi_pollfd *ipollfd;
        POLL_NFDS_TYPE nfds = 0;
-       struct pollfd *fds;
+       struct pollfd *fds = NULL;
        int i = -1;
        int timeout_ms;
 
@@ -1866,7 +1866,8 @@ static int handle_events(struct libusb_context *ctx, 
struct timeval *tv)
                nfds++;
 
        /* TODO: malloc when number of fd's changes, not on every poll */
-       fds = malloc(sizeof(*fds) * nfds);
+       if (nfds != 0)
+               fds = malloc(sizeof(*fds) * nfds);
        if (!fds) {
                usbi_mutex_unlock(&ctx->pollfds_lock);
                return LIBUSB_ERROR_NO_MEM;
@@ -2272,7 +2273,7 @@ int API_EXPORTED libusb_get_next_timeout(libusb_context 
*ctx,
        r = usbi_backend->clock_gettime(USBI_CLOCK_MONOTONIC, &cur_ts);
        if (r < 0) {
                usbi_err(ctx, "failed to read monotonic clock, errno=%d", 
errno);
-               return LIBUSB_ERROR_OTHER;
+               return 0;
        }
        TIMESPEC_TO_TIMEVAL(&cur_tv, &cur_ts);
 
-- 
1.7.10.msysgit.1

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to