9front - general discussion about 9front
 help / color / mirror / Atom feed
* [9front] [PATCH] git/fs: remove trailing null bytes from parent file
@ 2022-01-06  8:58 Michael Forney
  2022-01-06 16:07 ` ori
  0 siblings, 1 reply; 5+ messages in thread
From: Michael Forney @ 2022-01-06  8:58 UTC (permalink / raw)
  To: 9front


Previously, due to the way the size of buf was calculated, the parent
file had one trailing null byte for each parent.
---
diff 370bfd26ce5ffd9a06a314a20d1691cc6b15b712 2a5737d2ef4ea7920f6d4e5f47487837568b751c
--- a/sys/src/cmd/git/fs.c	Wed Jan  5 22:38:56 2022
+++ b/sys/src/cmd/git/fs.c	Thu Jan  6 00:52:21 2022
@@ -380,8 +380,8 @@
 	char *buf, *p;
 	int i, n;
 
-	n = o->commit->nparent * (40 + 2);
-	buf = emalloc(n);
+	n = o->commit->nparent * (40 + 1);
+	buf = emalloc(n + 1);
 	p = buf;
 	for (i = 0; i < o->commit->nparent; i++)
 		p += sprint(p, "%H\n", o->commit->parent[i]);

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

* Re: [9front] [PATCH] git/fs: remove trailing null bytes from parent file
  2022-01-06  8:58 [9front] [PATCH] git/fs: remove trailing null bytes from parent file Michael Forney
@ 2022-01-06 16:07 ` ori
  2022-01-06 16:49   ` Michael Forney
  0 siblings, 1 reply; 5+ messages in thread
From: ori @ 2022-01-06 16:07 UTC (permalink / raw)
  To: 9front

Quoth Michael Forney <mforney@mforney.org>:
> 
> Previously, due to the way the size of buf was calculated, the parent
> file had one trailing null byte for each parent.
> ---
> diff 370bfd26ce5ffd9a06a314a20d1691cc6b15b712 2a5737d2ef4ea7920f6d4e5f47487837568b751c
> --- a/sys/src/cmd/git/fs.c	Wed Jan  5 22:38:56 2022
> +++ b/sys/src/cmd/git/fs.c	Thu Jan  6 00:52:21 2022
> @@ -380,8 +380,8 @@
>  	char *buf, *p;
>  	int i, n;
>  
> -	n = o->commit->nparent * (40 + 2);
> -	buf = emalloc(n);
> +	n = o->commit->nparent * (40 + 1);
> +	buf = emalloc(n + 1);
>  	p = buf;
>  	for (i = 0; i < o->commit->nparent; i++)
>  		p += sprint(p, "%H\n", o->commit->parent[i]);
> 

I think this needs to be:

	n = o->commit->nparent * (40 + 1) + 1;

because we need 41 bytes per line *and* room
for the trailing nul.

While we're here, let's nuke the sprint, and
compute the number of bytes we wrote in.

diff 370bfd26ce5ffd9a06a314a20d1691cc6b15b712 uncommitted
--- a/sys/src/cmd/git/fs.c
+++ b/sys/src/cmd/git/fs.c
@@ -377,18 +377,18 @@
 static void
 readcommitparent(Req *r, Object *o)
 {
-	char *buf, *p;
+	char *buf, *p, *e;
 	int i, n;
 
-	n = o->commit->nparent * (40 + 2);
+	n = o->commit->nparent * (40 + 1) + 1;
 	buf = emalloc(n);
 	p = buf;
+	e = buf + n;
 	for (i = 0; i < o->commit->nparent; i++)
-		p += sprint(p, "%H\n", o->commit->parent[i]);
-	readbuf(r, buf, n);
+		p = seprint(p, e, "%H\n", o->commit->parent[i]);
+	readbuf(r, buf, p - buf);
 	free(buf);
 }
-


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

* Re: [9front] [PATCH] git/fs: remove trailing null bytes from parent file
  2022-01-06 16:07 ` ori
@ 2022-01-06 16:49   ` Michael Forney
  2022-01-06 16:55     ` ori
  2022-01-06 17:02     ` ori
  0 siblings, 2 replies; 5+ messages in thread
From: Michael Forney @ 2022-01-06 16:49 UTC (permalink / raw)
  To: 9front

On 2022-01-06, ori@eigenstate.org <ori@eigenstate.org> wrote:
> I think this needs to be:
>
> 	n = o->commit->nparent * (40 + 1) + 1;
>
> because we need 41 bytes per line *and* room
> for the trailing nul.
>
> While we're here, let's nuke the sprint, and
> compute the number of bytes we wrote in.

In my patch I had allocated n + 1 bytes for the trailing nul.

But regardless, your patch looks fine as well.

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

* Re: [9front] [PATCH] git/fs: remove trailing null bytes from parent file
  2022-01-06 16:49   ` Michael Forney
@ 2022-01-06 16:55     ` ori
  2022-01-06 17:02     ` ori
  1 sibling, 0 replies; 5+ messages in thread
From: ori @ 2022-01-06 16:55 UTC (permalink / raw)
  To: 9front

Quoth Michael Forney <mforney@mforney.org>:
> In my patch I had allocated n + 1 bytes for the trailing nul.

Ah, so you did -- my bad.


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

* Re: [9front] [PATCH] git/fs: remove trailing null bytes from parent file
  2022-01-06 16:49   ` Michael Forney
  2022-01-06 16:55     ` ori
@ 2022-01-06 17:02     ` ori
  1 sibling, 0 replies; 5+ messages in thread
From: ori @ 2022-01-06 17:02 UTC (permalink / raw)
  To: 9front

Quoth Michael Forney <mforney@mforney.org>:
> In my patch I had allocated n + 1 bytes for the trailing nul.

Ah, so you did -- my bad. I still would
like to get rid of sprint here, though.


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

end of thread, other threads:[~2022-01-06 17:49 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-06  8:58 [9front] [PATCH] git/fs: remove trailing null bytes from parent file Michael Forney
2022-01-06 16:07 ` ori
2022-01-06 16:49   ` Michael Forney
2022-01-06 16:55     ` ori
2022-01-06 17:02     ` ori

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).