From: Colin Percival Date: Tue, 16 Aug 2011 04:30:48 +0000 (-0400) Subject: Don't leak memory if realloc fails. X-Git-Tag: v3.0.0a~130 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a43875d305c005bebfbe78d1a2e2197ab39210e7;p=thirdparty%2Flibarchive.git Don't leak memory if realloc fails. Via: Tarsnap SVN-Revision: 3612 --- diff --git a/libarchive/archive_read_disk_set_standard_lookup.c b/libarchive/archive_read_disk_set_standard_lookup.c index 7e9a671c0..3bc52c70d 100644 --- a/libarchive/archive_read_disk_set_standard_lookup.c +++ b/libarchive/archive_read_disk_set_standard_lookup.c @@ -187,6 +187,8 @@ static const char * lookup_uname_helper(struct name_cache *cache, id_t id) { struct passwd pwent, *result; + char * nbuff; + size_t nbuff_size; int r; if (cache->buff_size == 0) { @@ -208,10 +210,12 @@ lookup_uname_helper(struct name_cache *cache, id_t id) * we just double it and try again. Because the buffer * is kept around in the cache object, we shouldn't * have to do this very often. */ - cache->buff_size *= 2; - cache->buff = realloc(cache->buff, cache->buff_size); - if (cache->buff == NULL) + nbuff_size = cache->buff_size * 2; + nbuff = realloc(cache->buff, nbuff_size); + if (nbuff == NULL) break; + cache->buff = nbuff; + cache->buff_size = nbuff_size; } if (r != 0) { archive_set_error(cache->archive, errno, @@ -251,6 +255,8 @@ static const char * lookup_gname_helper(struct name_cache *cache, id_t id) { struct group grent, *result; + char * nbuff; + size_t nbuff_size; int r; if (cache->buff_size == 0) { @@ -270,10 +276,12 @@ lookup_gname_helper(struct name_cache *cache, id_t id) /* ERANGE means our buffer was too small, but POSIX * doesn't tell us how big the buffer should be, so * we just double it and try again. */ - cache->buff_size *= 2; - cache->buff = realloc(cache->buff, cache->buff_size); - if (cache->buff == NULL) + nbuff_size = cache->buff_size * 2; + nbuff = realloc(cache->buff, nbuff_size); + if (nbuff == NULL) break; + cache->buff = nbuff; + cache->buff_size = nbuff_size; } if (r != 0) { archive_set_error(cache->archive, errno,