From a983fad006fe39a48517e061bf9f66501ff900be Mon Sep 17 00:00:00 2001 From: Pete Batard Date: Thu, 7 Jun 2012 19:27:43 +0100 Subject: [PATCH] All: Prevent memory leaks on realloc failures * 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 | 2 +- libusb/descriptor.c | 2 +- libusb/libusbi.h | 8 ++++++++ libusb/os/windows_usb.c | 2 +- libusb/version_nano.h | 2 +- 5 files changed, 12 insertions(+), 4 deletions(-) diff --git a/libusb/core.c b/libusb/core.c index a286da0..b0fa1c0 100644 --- a/libusb/core.c +++ b/libusb/core.c @@ -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; diff --git a/libusb/descriptor.c b/libusb/descriptor.c index 590d1dc..763f93c 100644 --- a/libusb/descriptor.c +++ b/libusb/descriptor.c @@ -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) { diff --git a/libusb/libusbi.h b/libusb/libusbi.h index 9de56a1..41a6bf0 100644 --- a/libusb/libusbi.h +++ b/libusb/libusbi.h @@ -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) );}) diff --git a/libusb/os/windows_usb.c b/libusb/os/windows_usb.c index dbebfaf..98b26eb 100644 --- a/libusb/os/windows_usb.c +++ b/libusb/os/windows_usb.c @@ -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); diff --git a/libusb/version_nano.h b/libusb/version_nano.h index f4b4197..ee5933f 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 10524 +#define LIBUSB_NANO 10525 -- 2.7.4