"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Omar Polo <op@omarpolo.com>
Subject:
Re: histedit_parse_list: avoid needless free(line)
To:
Theo Buehler <tb@theobuehler.org>
Cc:
gameoftrees@openbsd.org
Date:
Mon, 27 Feb 2023 10:25:55 +0100

Download raw body.

Thread
On 2023/02/27 09:27:51 +0100, Theo Buehler <tb@theobuehler.org> wrote:
> On Mon, Feb 27, 2023 at 09:10:49AM +0100, Omar Polo wrote:
> > Don't need to free(line) at every iteration, getline(3) allows to
> > re-use the storage safely (since it tracks linesize too.)  It
> > simplifies a bit the code.
> > 
> > ok?
> 
> Yes, ok.
> 
> I think size should be initialized to 0 at the top of the function - our
> getline does the right thing if *lineptr is NULL, but the manual says
> 
>   If *n is non-zero, then *lineptr must be pre-allocated to at least *n
>   bytes.

Oops, missed that size (now linesize) was left un-initialized.

> Also, maybe call it linesize instead of size like in all other getline()
> calls in this giant file.

I've committed with your suggestion, thanks!

This prompted me to go back and re-check the various getline() calls.
All of them seems fine (both line and linesize initialized to 0/NULL)
except for one in diff3.c:

diff /home/op/w/got
commit - e96d39bf87c8025d794e9c5c5bfcbbca58c216ac
path + /home/op/w/got
blob - ff8cf0b4e609ae11ccb229f749a19390a8a22563
file + lib/diff3.c
--- lib/diff3.c
+++ lib/diff3.c
@@ -689,17 +689,17 @@ get_line(char **ret, FILE *b, size_t *n, struct diff3_
 }
 
 static const struct got_error *
 get_line(char **ret, FILE *b, size_t *n, struct diff3_state *d3s)
 {
 	const struct got_error *err = NULL;
 	char *cp = NULL;
 	size_t size;
-	ssize_t len;
+	ssize_t len = 0;
 	char *new;
 
 	*ret = NULL;
 	if (n != NULL)
 		*n = 0;
 
 	len = getline(&cp, &size, b);
 	if (len == -1) {