All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] android/client: Fix memory leak while using realloc()
@ 2015-07-27  4:14 Atul Rai
  2015-07-27 19:18 ` Szymon Janc
  0 siblings, 1 reply; 2+ messages in thread
From: Atul Rai @ 2015-07-27  4:14 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: sachin.dev

While reallocating space to store additional "remote device set" using
realloc, if realloc() fails, the original block is left untouched but
reference to that block is lost as NULL is assigned to remote_devices.
The original block needs to be freed before return.
---
 android/client/if-bt.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/android/client/if-bt.c b/android/client/if-bt.c
index 4723024..34611b9 100644
--- a/android/client/if-bt.c
+++ b/android/client/if-bt.c
@@ -119,9 +119,18 @@ void add_remote_device(const bt_bdaddr_t *addr)
 	/* Realloc space if needed */
 	if (remote_devices_cnt >= remote_devices_capacity) {
 		remote_devices_capacity *= 2;
+		bt_bdaddr_t *tmp;
+
+		/*
+		 * Save reference to previously allocated memory block so that
+		 * it can be freed in case realloc fails.
+		 */
+		tmp = remote_devices;
+
 		remote_devices = realloc(remote_devices, sizeof(bt_bdaddr_t) *
 						remote_devices_capacity);
 		if (remote_devices == NULL) {
+			free(tmp);
 			remote_devices_capacity = 0;
 			remote_devices_cnt = 0;
 			return;
-- 
2.1.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] android/client: Fix memory leak while using realloc()
  2015-07-27  4:14 [PATCH v2] android/client: Fix memory leak while using realloc() Atul Rai
@ 2015-07-27 19:18 ` Szymon Janc
  0 siblings, 0 replies; 2+ messages in thread
From: Szymon Janc @ 2015-07-27 19:18 UTC (permalink / raw)
  To: Atul Rai; +Cc: linux-bluetooth, sachin.dev

Hi Atul,

On Monday 27 of July 2015 09:44:40 Atul Rai wrote:
> While reallocating space to store additional "remote device set" using
> realloc, if realloc() fails, the original block is left untouched but
> reference to that block is lost as NULL is assigned to remote_devices.
> The original block needs to be freed before return.
> ---
>  android/client/if-bt.c | 9 +++++++++
>  1 file changed, 9 insertions(+)
> 
> diff --git a/android/client/if-bt.c b/android/client/if-bt.c
> index 4723024..34611b9 100644
> --- a/android/client/if-bt.c
> +++ b/android/client/if-bt.c
> @@ -119,9 +119,18 @@ void add_remote_device(const bt_bdaddr_t *addr)
>  	/* Realloc space if needed */
>  	if (remote_devices_cnt >= remote_devices_capacity) {
>  		remote_devices_capacity *= 2;
> +		bt_bdaddr_t *tmp;
> +
> +		/*
> +		 * Save reference to previously allocated memory block so that
> +		 * it can be freed in case realloc fails.
> +		 */
> +		tmp = remote_devices;
> +
>  		remote_devices = realloc(remote_devices, sizeof(bt_bdaddr_t) *
>  						remote_devices_capacity);
>  		if (remote_devices == NULL) {
> +			free(tmp);
>  			remote_devices_capacity = 0;
>  			remote_devices_cnt = 0;
>  			return;


This doesn't compile cleanly:

  CC       android/client/android_haltest-if-hf-client.o
android/client/if-bt.c: In function ‘add_remote_device’:
android/client/if-bt.c:122:3: error: ISO C90 forbids mixed declarations and 
code [-Werror=declaration-after-statement]
   bt_bdaddr_t *tmp;
   ^
cc1: all warnings being treated as errors
Makefile:6171: recipe for target 'android/client/android_haltest-if-bt.o' 
failed

You can use ./boostrap-configure;make for testing patches as this enables 
extra compiler flags.

-- 
BR
Szymon Janc

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-07-27 19:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-27  4:14 [PATCH v2] android/client: Fix memory leak while using realloc() Atul Rai
2015-07-27 19:18 ` Szymon Janc

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.