From: Jim Meyering Date: Sun, 18 Apr 2004 14:58:51 +0000 (+0000) Subject: (full_filename_): Don't leak upon failed realloc. X-Git-Tag: v5.3.0~1749 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=544a43b5f7c93298abccb3ccc6ced841bf6923a9;p=thirdparty%2Fcoreutils.git (full_filename_): Don't leak upon failed realloc. --- diff --git a/src/remove.c b/src/remove.c index 4672e8a933..d9fb21796e 100644 --- a/src/remove.c +++ b/src/remove.c @@ -290,10 +290,10 @@ full_filename_ (Dirstack_state const *ds, const char *filename) error involving a file name to be expanded here wouldn't ever be issued. Use realloc and fall back on using a static buffer if memory allocation fails. */ - buf = realloc (buf, n_bytes_needed); + char *new_buf = realloc (buf, n_bytes_needed); n_allocated = n_bytes_needed; - if (buf == NULL) + if (new_buf == NULL) { #define SBUF_SIZE 512 #define ELLIPSES_PREFIX "[...]" @@ -302,6 +302,7 @@ full_filename_ (Dirstack_state const *ds, const char *filename) size_t len; char *p; + free (buf); len = right_justify (static_buf, SBUF_SIZE, filename, filename_len + 1, &p, &truncated); right_justify (static_buf, len, dir_name, dir_len, &p, &truncated); @@ -312,6 +313,8 @@ full_filename_ (Dirstack_state const *ds, const char *filename) } return p; } + + buf = new_buf; } if (filename_len == 1 && *filename == '.' && dir_len)