All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vinicius Costa Gomes <vinicius.gomes@intel.com>
To: Atul Rai <a.rai@samsung.com>, linux-bluetooth@vger.kernel.org
Cc: sachin.dev@samsung.com
Subject: Re: [PATCH] android/client: Fix memory leak while using realloc()
Date: Fri, 24 Jul 2015 14:07:27 -0300	[thread overview]
Message-ID: <87vbd94h80.fsf@intel.com> (raw)
In-Reply-To: <1437553983-11102-1-git-send-email-a.rai@samsung.com>

Hi,

Atul Rai <a.rai@samsung.com> writes:

> 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 | 8 ++++++++
>  1 file changed, 8 insertions(+)
>
> diff --git a/android/client/if-bt.c b/android/client/if-bt.c
> index 4723024..4249f78 100644
> --- a/android/client/if-bt.c
> +++ b/android/client/if-bt.c
> @@ -94,6 +94,7 @@ static int remote_devices_capacity = 0;
>  void add_remote_device(const bt_bdaddr_t *addr)
>  {
>  	int i;
> +	bt_bdaddr_t *tmp;
>
>  	if (remote_devices == NULL) {
>  		remote_devices = malloc(4 * sizeof(bt_bdaddr_t));
> @@ -119,9 +120,16 @@ void add_remote_device(const bt_bdaddr_t *addr)
>  	/* Realloc space if needed */
>  	if (remote_devices_cnt >= remote_devices_capacity) {

I would move 'tmp' declaration here. Makes it clear that it is only used
in this context.

Apart from that, looks OK.

>  		remote_devices_capacity *= 2;
> +		/*
> +		 * 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


Cheers,
--
Vinicius

  reply	other threads:[~2015-07-24 17:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-22  8:33 [PATCH] android/client: Fix memory leak while using realloc() Atul Rai
2015-07-24 17:07 ` Vinicius Costa Gomes [this message]
  -- strict thread matches above, loose matches on Subject: below --
2015-07-27  4:10 Atul Rai

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87vbd94h80.fsf@intel.com \
    --to=vinicius.gomes@intel.com \
    --cc=a.rai@samsung.com \
    --cc=linux-bluetooth@vger.kernel.org \
    --cc=sachin.dev@samsung.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.