diff options
author | Pete Batard <pete@akeo.ie> | 2012-06-07 19:27:43 +0100 |
---|---|---|
committer | Pete Batard <pete@akeo.ie> | 2012-06-08 23:30:54 +0100 |
commit | a983fad006fe39a48517e061bf9f66501ff900be (patch) | |
tree | 7077240159f3ae43089f10f7ae642fb56930c88e /libusb/libusbi.h | |
parent | ff3259b440b3fa55a48cfb6233341c9ff664894a (diff) | |
download | libusb-a983fad006fe39a48517e061bf9f66501ff900be.tar.gz |
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)
Diffstat (limited to 'libusb/libusbi.h')
-rw-r--r-- | libusb/libusbi.h | 8 |
1 files changed, 8 insertions, 0 deletions
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) );}) |