mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Hao Zeng <zenghao@kylinos.cn>
Cc: chenhuacai@kernel.org, zhangqing@loongson.cn,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] recordmcount: Fix memory leaks in the uwrite function
Date: Mon, 24 Apr 2023 16:06:46 -0400	[thread overview]
Message-ID: <20230424160646.3faf00f1@rorschach.local.home> (raw)
In-Reply-To: <20230412093048.3005276-1-zenghao@kylinos.cn>

On Wed, 12 Apr 2023 17:30:48 +0800
Hao Zeng <zenghao@kylinos.cn> wrote:

> Common realloc mistake: 'file_append' nulled but not freed upon failure
> 
> Signed-off-by: Hao Zeng <zenghao@kylinos.cn>
> ---
>  scripts/recordmcount.c | 17 +++++++++--------
>  1 file changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/scripts/recordmcount.c b/scripts/recordmcount.c
> index e30216525325..2b7173a86d4c 100644
> --- a/scripts/recordmcount.c
> +++ b/scripts/recordmcount.c
> @@ -110,22 +110,23 @@ static ssize_t uwrite(void const *const buf, size_t const count)
>  {
>  	size_t cnt = count;
>  	off_t idx = 0;
> -
> +	void *p = NULL;
>  	file_updated = 1;
>  
>  	if (file_ptr + count >= file_end) {
>  		off_t aoffset = (file_ptr + count) - file_end;
>  
>  		if (aoffset > file_append_size) {
> -			file_append = realloc(file_append, aoffset);
> +			p = realloc(file_append, aoffset);
> +			if (!p) {
> +				perror("write");
> +				file_append_cleanup();
> +				mmap_cleanup();
> +				return -1;
> +			}
> +			file_append = p;
>  			file_append_size = aoffset;
>  		}

This changes the logic of the function. If file_append is NULL when
entering, and does not get into the allocate path we still want this to
error.

Just do:

		p = realloc(file_append, aoffset);
		if (!p) {
			free(file_append);
			file_append = NULL;
		}

And that keeps the same logic but removes the memory leak.

-- Steve


> -		if (!file_append) {
> -			perror("write");
> -			file_append_cleanup();
> -			mmap_cleanup();
> -			return -1;
> -		}
>  		if (file_ptr < file_end) {
>  			cnt = file_end - file_ptr;
>  		} else {


      reply	other threads:[~2023-04-24 20:06 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-12  9:30 Hao Zeng
2023-04-24 20:06 ` Steven Rostedt [this message]

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=20230424160646.3faf00f1@rorschach.local.home \
    --to=rostedt@goodmis.org \
    --cc=chenhuacai@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zenghao@kylinos.cn \
    --cc=zhangqing@loongson.cn \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®