diff options
author | Guillem Jover <guillem@hadrons.org> | 2013-06-01 18:00:15 +0200 |
---|---|---|
committer | Samuel Ortiz <sameo@linux.intel.com> | 2013-06-14 17:43:45 +0200 |
commit | 26a97221b86ef958715a1c354331432aa71fd64a (patch) | |
tree | 6f3cd6515300f6aeb32935d544c0a1a817e1d9fe | |
parent | bc1e1caf615defa9b287bfc0ff047f1150e0bd4a (diff) | |
download | neard-26a97221b86ef958715a1c354331432aa71fd64a.tar.gz neard-26a97221b86ef958715a1c354331432aa71fd64a.tar.bz2 neard-26a97221b86ef958715a1c354331432aa71fd64a.zip |
npp: Fix memory leak on allocation error
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
-rw-r--r-- | plugins/npp.c | 7 |
1 files 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); |