From ed298d6c8ed7e6dc70b6e71947c55a8266ea0503 Mon Sep 17 00:00:00 2001
From: Guillem Jover <guillem@hadrons.org>
Date: Fri, 31 May 2013 21:29:40 +0200
Subject: [PATCH] npp: Fix memory leak on allocation error
Status: applied

The pointer to a realloc() function does not get touched on error, so
we need to check if the function failed and either free or update the
pointer.

Warned-by: cppcheck
---
 plugins/npp.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/plugins/npp.c b/plugins/npp.c
index b5a12ff..e8bcd19 100644
--- a/plugins/npp.c
+++ b/plugins/npp.c
@@ -61,7 +61,7 @@ static near_bool_t npp_read(int client_fd,
 	struct p2p_npp_frame frame;
 	struct p2p_npp_ndef_entry entry;
 	int bytes_recv, n_ndef, i, ndef_length, total_ndef_length, err;
-	uint8_t *ndefs, *current_ndef;
+	uint8_t *ndefs, *new_ndefs, *current_ndef;
 	GList *records;
 
 	ndefs = NULL;
@@ -91,13 +91,14 @@ static near_bool_t npp_read(int client_fd,
 		total_ndef_length += ndef_length + TLV_SIZE;
 		DBG("NDEF %d length %d", i, ndef_length);
 
-		ndefs = g_try_realloc(ndefs, total_ndef_length);
-		if (ndefs == NULL) {
+		new_ndefs = g_try_realloc(ndefs, total_ndef_length);
+		if (new_ndefs == NULL) {
 			near_error("Could not allocate NDEF buffer %d",
 								bytes_recv);
 			err = -ENOMEM;
 			break;
 		}
+		ndefs = new_ndefs;
 
 		current_ndef = ndefs + total_ndef_length
 					- (ndef_length + TLV_SIZE);
-- 
1.8.3

