mailing list of musl libc
 help / color / mirror / code / Atom feed
From: jason <jason@insomnia247.nl>
To: musl@lists.openwall.com
Cc: jason@insomnia247.nl
Subject: [musl] Bug in src/stdio/fread.c
Date: Sat, 10 Jul 2021 15:10:26 +0200 (CEST)	[thread overview]
Message-ID: <20210710131026.AE6BD22201B9@gateway02.insomnia247.nl> (raw)

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


             reply	other threads:[~2021-07-10 14:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-10 13:10 jason [this message]
2021-07-10 16:50 ` Rich Felker
2021-07-10 17:13   ` David Edelsohn
2021-07-10 17:17     ` David Edelsohn

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=20210710131026.AE6BD22201B9@gateway02.insomnia247.nl \
    --to=jason@insomnia247.nl \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

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

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