From dae1f953717f2c49f7a4ae34e21c3c93117d1e64 Mon Sep 17 00:00:00 2001 From: Eduardo Sanchez Date: Mon, 1 Jun 2015 00:04:31 -0300 Subject: [PATCH] Adding a new temporary variable for realloc memory in order to keep the reference to the allocated memory before the function returns. In case that realloc fails (i.e. OOM), before exiting with "return(1)", the application is still able to free the corresponding allocated memory. Signed-off-by: Eduardo Sanchez --- client/pub_client.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/client/pub_client.c b/client/pub_client.c index d48cd71c..b29dd60e 100644 --- a/client/pub_client.c +++ b/client/pub_client.c @@ -128,15 +128,20 @@ int load_stdin(void) { long pos = 0, rlen; char buf[1024]; + char *aux_message = NULL; mode = MSGMODE_STDIN_FILE; while(!feof(stdin)){ rlen = fread(buf, 1, 1024, stdin); - message = realloc(message, pos+rlen); - if(!message){ + aux_message = realloc(message, pos+rlen); + if(!aux_message){ if(!quiet) fprintf(stderr, "Error: Out of memory.\n"); + free(message); return 1; + } else + { + message = aux_message; } memcpy(&(message[pos]), buf, rlen); pos += rlen;