9front - general discussion about 9front
 help / color / mirror / Atom feed
From: "Igor Boehm" <boehm.igor@gmail.com>
To: 9front@9front.org
Subject: Re: [9front] cmd/sort: fix resource leak (patch)
Date: Fri, 01 Jan 2021 20:32:03 +0100	[thread overview]
Message-ID: <E18F43DD482FE268E9EEC37A6FF77927@gmail.com> (raw)
In-Reply-To: <18544CC779308C2AA34DBCFF8C5ADF50@eigenstate.org>

>Quoth Igor Boehm <boehm.igor@gmail.com>:
>> diff -r 4c6206d69abb sys/src/cmd/sort.c
>> --- a /sys/src/cmd/sort.c	Mon Dec 28 21:21:22 2020 +0100
>> +++ b/sys/src/cmd/sort.c	Tue Dec 29 15:11:29 2020 +0100
>> @@ -209,6 +209,7 @@
>>  			free(ol);
>>  			ol = l;
>>  		}
>> +		free(ol);
>>  		return;
>>  	}
>
>Am I wrong in thinking that we should also be
>freeing ol->key?

Good catch Ori!  Me must have been blind when reviewing this ~SIGH~

Here the reasoning spelled out:

'ol->key' is either heap allocated or set to '0' in function
/sys/src/cmd/sort.c:^buildkey.

The buildkey function is called from /sys/src/cmd/sort.c:^newline and
I believe that on all its exit paths where allocation of the line
succeeds, buildkey() is called.

Hence it is prudent to call free on it as you suggest (if ol->key is
'0' the free() acts as a noop).

So the patch should look like this:

diff -r 4c6206d69abb sys/src/cmd/sort.c
--- a/sys/src/cmd/sort.c	Mon Dec 28 21:21:22 2020 +0100
+++ b/sys/src/cmd/sort.c	Fri Jan 01 20:28:23 2021 +0100
@@ -209,6 +209,8 @@
 			free(ol);
 			ol = l;
 		}
+		free(ol->key);
+		free(ol);
 		return;
 	}


  reply	other threads:[~2021-01-01 19:34 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-29 14:28 Igor Boehm
2021-01-01 17:42 ` ori
2021-01-01 19:32   ` Igor Boehm [this message]
2021-01-01 19:49     ` ori

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=E18F43DD482FE268E9EEC37A6FF77927@gmail.com \
    --to=boehm.igor@gmail.com \
    --cc=9front@9front.org \
    /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;
as well as URLs for NNTP newsgroup(s).