Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
mlq4's patch for #27
From 16cebba315614cd527976f6625b3ac7661ed2f47 Mon Sep 17 00:00:00 2001
From: Moritz Lipp <mlq@pwmt.org>
Date: Thu, 7 Jun 2012 01:08:53 +0200
Subject: [PATCH] Free 'altsetting' upon failed realloc
If the reallocation of 'altsetting' fails the variable gets set to NULL
while the memory it originally pointed to remains untouched. This patch
resolves this common error by introducing a temporary variable that is
used to catch the failed reallocation and freeing the previously
allocated memory.
---
libusb/descriptor.c | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/libusb/descriptor.c b/libusb/descriptor.c
index 590d1dc..ba25f36 100644
--- a/libusb/descriptor.c
+++ b/libusb/descriptor.c
@@ -198,12 +198,15 @@ 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,
+ struct libusb_interface_descriptor *tmp = realloc(altsetting,
sizeof(struct libusb_interface_descriptor) *
(usb_interface->num_altsetting + 1));
- if (!altsetting) {
+ if (!tmp) {
+ free(altsetting);
r = LIBUSB_ERROR_NO_MEM;
goto err;
+ } else {
+ altsetting = tmp;
}
usb_interface->altsetting = altsetting;
--
1.7.10.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment