From: "adr via 9fans" <9fans@9fans.net>
To: 9fans <9fans@9fans.net>
Subject: Re: [9fans] pngread: alloc chunk's length
Date: Mon, 12 Jul 2021 16:28:43 +0000 (UTC) [thread overview]
Message-ID: <625366cf-4157-435c-4f87-e176eb3c9d66@SDF.ORG> (raw)
In-Reply-To: <F07EC11FF3FC141AE041C38885D93D5F@eigenstate.org>
On Mon, 12 Jul 2021, ori@eigenstate.org wrote:
> Why not make getchunk allocate? Somethign like:
>
> ---
> //.git/fs/object/e8259861da3a55c03491904e4d11c5c15b7577c5/tree/sys/src/cmd/jpg/readpng.c
> +++ sys/src/cmd/jpg/readpng.c
> @@ -94,7 +94,7 @@
> }
>
> static int
> -getchunk(Biobuf *b, char *type, uchar *d, int m)
> +getchunk(Biobuf *b, char *type, uchar **d)
> {
> uchar buf[8];
> ulong crc = 0, crc2;
> @@ -103,11 +103,10 @@
> if(Bread(b, buf, 8) != 8)
> return -1;
> n = get4(buf);
> + *d = pngmalloc(n, 0);
> memmove(type, buf+4, 4);
> type[4] = 0;
> - if(n > m)
> - sysfatal("getchunk needed %d, had %d", n, m);
> - nr = Bread(b, d, n);
> + nr = Bread(b, *d, n);
> if(nr != n)
> sysfatal("getchunk read %d, expected %d", nr, n);
> crc = blockcrc(crctab, crc, type, 4);
> @@ -131,7 +130,7 @@
> Again:
> z->p = z->buf;
> z->e = z->p;
> - n = getchunk(z->io, type, z->p, IDATSIZE);
> + n = getchunk(z->io, type, &z->p);
> if(n < 0 || strcmp(type, "IEND") == 0)
> return -1;
> z->e = z->p + n;
>
>
Hi Ori,
I thought that it would be a good idea to have a separate function
to just get the size without squeezing the fs through all the chunk,
it could be useful in the future. getchunk() could call it, but I
didn't see the point. I imagine that this is not a complete patch
but a suggestion, but anyway remember that you don't need z->buf
any more, and don't forget to free z->p for the next iteration.
By the way after revisiting readpng.c I noticed that I checked for
a negative value of the IHDR chunk length, better do like in the
original code:
[...]
n = chunklen(b);
if(n < 13)
sysfatal("missing IHDR chunk");
[...]
Regards,
adr.
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/T4a714ed14c50767a-M36acff490ae45918672f52bd
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription
next prev parent reply other threads:[~2021-07-12 16:28 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-10 21:28 adr via 9fans
2021-07-10 21:49 ` adr via 9fans
2021-07-12 12:53 ` ori
2021-07-12 16:28 ` adr via 9fans [this message]
2021-07-12 16:01 ` ori
2021-07-12 17:01 ` adr via 9fans
2021-07-12 18:04 ` hiro
2021-07-12 23:31 ` adr via 9fans
2021-07-12 23:49 ` adr via 9fans
2021-07-13 8:16 ` hiro
2021-07-13 9:40 ` adr via 9fans
2021-07-13 9:46 ` hiro
2021-07-13 12:04 ` adr via 9fans
2021-07-13 12:11 ` adr via 9fans
2021-07-14 15:52 ` ori
2021-07-14 17:05 ` adr via 9fans
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=625366cf-4157-435c-4f87-e176eb3c9d66@SDF.ORG \
--to=9fans@9fans.net \
/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).