diff options
author | Rikard Falkeborn <rikard.falkeborn@gmail.com> | 2020-04-13 07:28:48 +0200 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2020-04-13 15:44:46 +0200 |
commit | 1b3289745334ec31507a12b6c54b2883a521543e (patch) | |
tree | fea28e9b2f68178c5252f3c68cedccc1f766859f /lib/libalpm/util.c | |
parent | 1d39557aa070d2260cfda650e59c8d190397ba01 (diff) | |
download | pacman-1b3289745334ec31507a12b6c54b2883a521543e.tar.gz pacman-1b3289745334ec31507a12b6c54b2883a521543e.tar.xz |
Add REALLOC macro to simplify realloc error handling
realloc can fail just like the other memory allocation functions. Add a
macro to simplify handling of realloc failures, similar to the already
existing MALLOC, CALLOC, etc.
Replace the existing realloc uses with the new macro, allowing us to
move tedious error handling to the macro. Also, in be_package and
be_sync, this fixes hypothetical memory leaks (and thereafter null
pointer dereferences) in case realloc fails to shrink the allocated
memory.
Signed-off-by: Rikard Falkeborn <rikard.falkeborn@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'lib/libalpm/util.c')
-rw-r--r-- | lib/libalpm/util.c | 13 |
1 files changed, 3 insertions, 10 deletions
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c index c46b1397..cebc87ec 100644 --- a/lib/libalpm/util.c +++ b/lib/libalpm/util.c @@ -1427,22 +1427,15 @@ int _alpm_fnmatch(const void *pattern, const void *string) */ void *_alpm_realloc(void **data, size_t *current, const size_t required) { - char *newdata; - - newdata = realloc(*data, required); - if(!newdata) { - _alpm_alloc_fail(required); - return NULL; - } + REALLOC(*data, required, return NULL); if (*current < required) { /* ensure all new memory is zeroed out, in both the initial * allocation and later reallocs */ - memset(newdata + *current, 0, required - *current); + memset((char*)*data + *current, 0, required - *current); } *current = required; - *data = newdata; - return newdata; + return *data; } /** This automatically grows data based on current/required. |