mailing list of musl libc
 help / color / mirror / code / 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

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

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