Created
July 5, 2012 15:03
-
-
Save pbatard/3054211 to your computer and use it in GitHub Desktop.
improvement on the earlier patch with usbi_reallocf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From c85a751dfd6519d126772bab7796e96c4c63351f Mon Sep 17 00:00:00 2001 | |
From: Pete Batard <pete@akeo.ie> | |
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 +- | |
4 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); | |
-- | |
1.7.10.msysgit.1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment