mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] Bug in src/stdio/fread.c
@ 2021-07-10 13:10 jason
  2021-07-10 16:50 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: jason @ 2021-07-10 13:10 UTC (permalink / raw)
  To: musl; +Cc: jason

If you look at the code:

size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
{
	unsigned char *dest = destv;
	size_t len = size*nmemb, l = len, k;
	if (!size) nmemb = 0;

	FLOCK(f);

	f->mode |= f->mode-1;

	if (f->rpos != f->rend) {
		/* First exhaust the buffer. */
		k = MIN(f->rend - f->rpos, l);
		memcpy(dest, f->rpos, k);
		f->rpos += k;
		dest += k;
		l -= k;
	}
	
	/* Read the remainder directly */
	for (; l; l-=k, dest+=k) {
		k = __toread(f) ? 0 : f->read(f, dest, l);
		if (!k) {
			FUNLOCK(f);
			return (len-l)/size;
		}
	}

	FUNLOCK(f);
	return nmemb;
}

Consider what happens when f->rpos == f->rend: k is used uninitialized.

My suggested fix is:
-	if (f->rpos != f->rend) {
+	k = f->rend - f->rpos;
+	if (!k) {
 		/* First exhaust the buffer. */
-		k = MIN(f->rend - f->rpos, l);
+		k = MIN(k, l);


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

* Re: [musl] Bug in src/stdio/fread.c
  2021-07-10 13:10 [musl] Bug in src/stdio/fread.c jason
@ 2021-07-10 16:50 ` Rich Felker
  2021-07-10 17:13   ` David Edelsohn
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2021-07-10 16:50 UTC (permalink / raw)
  To: jason; +Cc: musl

On Sat, Jul 10, 2021 at 03:10:26PM +0200, jason wrote:
> If you look at the code:
> 
> size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
> {
> 	unsigned char *dest = destv;
> 	size_t len = size*nmemb, l = len, k;
> 	if (!size) nmemb = 0;
> 
> 	FLOCK(f);
> 
> 	f->mode |= f->mode-1;
> 
> 	if (f->rpos != f->rend) {
> 		/* First exhaust the buffer. */
> 		k = MIN(f->rend - f->rpos, l);
> 		memcpy(dest, f->rpos, k);
> 		f->rpos += k;
> 		dest += k;
> 		l -= k;
> 	}
> 	
> 	/* Read the remainder directly */
> 	for (; l; l-=k, dest+=k) {
> 		k = __toread(f) ? 0 : f->read(f, dest, l);
> 		if (!k) {
> 			FUNLOCK(f);
> 			return (len-l)/size;
> 		}
> 	}
> 
> 	FUNLOCK(f);
> 	return nmemb;
> }
> 
> Consider what happens when f->rpos == f->rend: k is used uninitialized.

At which line?

Rich

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

* Re: [musl] Bug in src/stdio/fread.c
  2021-07-10 16:50 ` Rich Felker
@ 2021-07-10 17:13   ` David Edelsohn
  2021-07-10 17:17     ` David Edelsohn
  0 siblings, 1 reply; 4+ messages in thread
From: David Edelsohn @ 2021-07-10 17:13 UTC (permalink / raw)
  To: musl; +Cc: jason

On Sat, Jul 10, 2021 at 12:51 PM Rich Felker <dalias@libc.org> wrote:
>
> On Sat, Jul 10, 2021 at 03:10:26PM +0200, jason wrote:
> > If you look at the code:
> >
> > size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
> > {
> >       unsigned char *dest = destv;

k declared but not initialized.

> >       size_t len = size*nmemb, l = len, k;
> >       if (!size) nmemb = 0;
> >
> >       FLOCK(f);
> >
> >       f->mode |= f->mode-1;
> >
> >       if (f->rpos != f->rend) {
> >               /* First exhaust the buffer. */

k set to value.

> >               k = MIN(f->rend - f->rpos, l);
> >               memcpy(dest, f->rpos, k);
> >               f->rpos += k;
> >               dest += k;
> >               l -= k;
> >       }
> >
> >       /* Read the remainder directly */

USE of k.  If f->rpos == f->rend, k was never set before use for the
first iteration of the loop.

> >       for (; l; l-=k, dest+=k) {
> >               k = __toread(f) ? 0 : f->read(f, dest, l);
> >               if (!k) {
> >                       FUNLOCK(f);
> >                       return (len-l)/size;
> >               }
> >       }
> >
> >       FUNLOCK(f);
> >       return nmemb;
> > }
> >
> > Consider what happens when f->rpos == f->rend: k is used uninitialized.
>
> At which line?
>
> Rich

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

* Re: [musl] Bug in src/stdio/fread.c
  2021-07-10 17:13   ` David Edelsohn
@ 2021-07-10 17:17     ` David Edelsohn
  0 siblings, 0 replies; 4+ messages in thread
From: David Edelsohn @ 2021-07-10 17:17 UTC (permalink / raw)
  To: musl; +Cc: jason

On Sat, Jul 10, 2021 at 1:13 PM David Edelsohn <dje.gcc@gmail.com> wrote:
>
> On Sat, Jul 10, 2021 at 12:51 PM Rich Felker <dalias@libc.org> wrote:
> >
> > On Sat, Jul 10, 2021 at 03:10:26PM +0200, jason wrote:
> > > If you look at the code:
> > >
> > > size_t fread(void *restrict destv, size_t size, size_t nmemb, FILE *restrict f)
> > > {
> > >       unsigned char *dest = destv;
>
> k declared but not initialized.
>
> > >       size_t len = size*nmemb, l = len, k;
> > >       if (!size) nmemb = 0;
> > >
> > >       FLOCK(f);
> > >
> > >       f->mode |= f->mode-1;
> > >
> > >       if (f->rpos != f->rend) {
> > >               /* First exhaust the buffer. */
>
> k set to value.
>
> > >               k = MIN(f->rend - f->rpos, l);
> > >               memcpy(dest, f->rpos, k);
> > >               f->rpos += k;
> > >               dest += k;
> > >               l -= k;
> > >       }
> > >
> > >       /* Read the remainder directly */
>
> USE of k.  If f->rpos == f->rend, k was never set before use for the
> first iteration of the loop.
>
> > >       for (; l; l-=k, dest+=k) {
> > >               k = __toread(f) ? 0 : f->read(f, dest, l);
> > >               if (!k) {
> > >                       FUNLOCK(f);
> > >                       return (len-l)/size;
> > >               }
> > >       }
> > >
> > >       FUNLOCK(f);
> > >       return nmemb;
> > > }
> > >
> > > Consider what happens when f->rpos == f->rend: k is used uninitialized.
> >
> > At which line?

Sorry, my mistake, k will be set in the loop before the iteration
expression is evaluated.

- David

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

end of thread, other threads:[~2021-07-10 17:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-10 13:10 [musl] Bug in src/stdio/fread.c jason
2021-07-10 16:50 ` Rich Felker
2021-07-10 17:13   ` David Edelsohn
2021-07-10 17:17     ` David Edelsohn

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ http://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git