]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Don't leak memory if realloc fails.
authorColin Percival <cperciva@daemonology.net>
Tue, 16 Aug 2011 04:30:48 +0000 (00:30 -0400)
committerColin Percival <cperciva@daemonology.net>
Tue, 16 Aug 2011 04:30:48 +0000 (00:30 -0400)
Via: Tarsnap

SVN-Revision: 3612

libarchive/archive_read_disk_set_standard_lookup.c

index 7e9a671c007d6521fa9a8b2a80cffbe7cdbf0a27..3bc52c70dbcbfefb16f8c6210925ea93774c239e 100644 (file)
@@ -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,