summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPete Batard <pete@akeo.ie>2012-06-07 19:27:43 +0100
committerPete Batard <pete@akeo.ie>2012-06-08 23:30:54 +0100
commita983fad006fe39a48517e061bf9f66501ff900be (patch)
tree7077240159f3ae43089f10f7ae642fb56930c88e
parentff3259b440b3fa55a48cfb6233341c9ff664894a (diff)
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)
-rw-r--r--libusb/core.c2
-rw-r--r--libusb/descriptor.c2
-rw-r--r--libusb/libusbi.h8
-rw-r--r--libusb/os/windows_usb.c2
-rw-r--r--libusb/version_nano.h2
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