From fe2bc30984af5487c1871f80c31d2911ec70bc89 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 7 Apr 2009 13:48:34 +0000 Subject: Fix a memory leak in realloc_buffer. If realloc fails, the original block is left untouched. So don't overwrite it that we can free it. git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@411 7dcaeef0-15fb-0310-b436-a5af3365683c --- libssh/buffer.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'libssh/buffer.c') diff --git a/libssh/buffer.c b/libssh/buffer.c index 7e4a80f..32a1f5b 100644 --- a/libssh/buffer.c +++ b/libssh/buffer.c @@ -68,16 +68,19 @@ void buffer_free(struct buffer_struct *buffer) { static int realloc_buffer(struct buffer_struct *buffer, int needed) { int smallest = 1; + char *new = NULL; /* Find the smallest power of two which is greater or equal to needed */ while(smallest <= needed) { smallest <<= 1; } needed = smallest; - buffer->data = realloc(buffer->data, needed); - if (buffer->data == NULL) { + new = realloc(buffer->data, needed); + if (new == NULL) { return -1; } + buffer->data = new; buffer->allocated = needed; + return 0; } -- cgit