All: Prevent memory leaks on realloc failures
authorPete Batard <pete@akeo.ie>
Thu, 7 Jun 2012 18:27:43 +0000 (19:27 +0100)
committerPete Batard <pete@akeo.ie>
Fri, 8 Jun 2012 22:30:54 +0000 (23:30 +0100)
* p = realloc(p, new_size) does not free the original buffer in case of
  a realloc failure.
* reallocf() can be used to do so, but is not available on all platforms.
* This patch introduces usbi_reallocf() in libusbi.h and use that instead of realloc
* Issue and original patch submitted by Moritz Lipp (trac #27)

libusb/core.c
libusb/descriptor.c
libusb/libusbi.h
libusb/os/windows_usb.c
libusb/version_nano.h

index a286da0..b0fa1c0 100644 (file)
@@ -472,7 +472,7 @@ struct discovered_devs *discovered_devs_append(
        /* exceeded capacity, need to grow */
        usbi_dbg("need to increase capacity");
        capacity = discdevs->capacity + DISCOVERED_DEVICES_SIZE_STEP;
-       discdevs = realloc(discdevs,
+       discdevs = usbi_reallocf(discdevs,
                sizeof(*discdevs) + (sizeof(void *) * capacity));
        if (discdevs) {
                discdevs->capacity = capacity;
index 590d1dc..763f93c 100644 (file)
@@ -198,7 +198,7 @@ static int parse_interface(libusb_context *ctx,
        while (size >= INTERFACE_DESC_LENGTH) {
                struct libusb_interface_descriptor *altsetting =
                        (struct libusb_interface_descriptor *) usb_interface->altsetting;
-               altsetting = realloc(altsetting,
+               altsetting = usbi_reallocf(altsetting,
                        sizeof(struct libusb_interface_descriptor) *
                        (usb_interface->num_altsetting + 1));
                if (!altsetting) {
index 9de56a1..41a6bf0 100644 (file)
@@ -110,6 +110,14 @@ static inline void list_del(struct list_head *entry)
        entry->prev->next = entry->next;
 }
 
+static inline void *usbi_reallocf(void *ptr, size_t size)
+{
+       void *ret = realloc(ptr, size);
+       if (!ret)
+               free(ptr);
+       return ret;
+}
+
 #define container_of(ptr, type, member) ({                      \
         const typeof( ((type *)0)->member ) *mptr = (ptr);    \
         (type *)( (char *)mptr - offsetof(type,member) );})
index dbebfaf..98b26eb 100644 (file)
@@ -1471,7 +1471,7 @@ static int windows_get_device_list(struct libusb_context *ctx, struct discovered
                                        unref_list[unref_cur++] = dev;
                                        if (unref_cur >= unref_size) {
                                                unref_size += 64;
-                                               unref_list = realloc(unref_list, unref_size*sizeof(libusb_device*));
+                                               unref_list = usbi_reallocf(unref_list, unref_size*sizeof(libusb_device*));
                                                if (unref_list == NULL) {
                                                        usbi_err(ctx, "could not realloc list for unref - aborting.");
                                                        LOOP_BREAK(LIBUSB_ERROR_NO_MEM);
index f4b4197..ee5933f 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10524
+#define LIBUSB_NANO 10525