Permalink
Browse files

Don't leak memory if realloc fails.

Via:	Tarsnap

SVN-Revision: 3612
  • Loading branch information...
1 parent 7c0891c commit a43875d305c005bebfbe78d1a2e2197ab39210e7 Colin Percival committed Aug 16, 2011
Showing with 14 additions and 6 deletions.
  1. +14 −6 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,

0 comments on commit a43875d

Please sign in to comment.