shithub: riscv

Download patch

ref: d0405d6c245bee5de92ffe1973294a3577af92c8
parent: c0c9a9927f413e70f59158a10c460b1927985f44
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Wed Oct 5 20:40:28 EDT 2011

jpg: use Breadn() instead if Bread() to guard aganst short reads

--- a/sys/src/cmd/jpg/ico.c
+++ b/sys/src/cmd/jpg/ico.c
@@ -58,7 +58,7 @@
 	uchar buf[40];
 
 	memset(h, 0, sizeof(*h));
-	if(Bread(b, buf, 6) != 6)
+	if(Breadn(b, buf, 6) != 6)
 		goto eof;
 	if(gets(&buf[0]) != 0)
 		goto header;
@@ -70,7 +70,7 @@
 		icon = mallocz(sizeof(*icon), 1);
 		if(icon == nil)
 			sysfatal("malloc: %r");
-		if(Bread(b, buf, 16) != 16)
+		if(Breadn(b, buf, 16) != 16)
 			goto eof;
 		icon->w = buf[0];
 		icon->h = buf[1];
@@ -195,7 +195,7 @@
 	buf = malloc(icon->len);
 	if(buf == nil)
 		return -1;
-	if(Bread(b, buf, icon->len) != icon->len){
+	if(Breadn(b, buf, icon->len) != icon->len){
 		werrstr("unexpected EOF");
 		return -1;
 	}
--- a/sys/src/cmd/jpg/readbmp.c
+++ b/sys/src/cmd/jpg/readbmp.c
@@ -320,7 +320,7 @@
 		for(iy = height; iy; iy--, i += step_up)
 			for(ix = 0; ix < width; ix++, i++) {
 				unsigned val;
-				Bread(b, c, sizeof(c));
+				Breadn(b, c, sizeof(c));
 				val = (unsigned)c[0] + ((unsigned)c[1] << 8);
 
 				buf[i].alpha = 0;
@@ -340,7 +340,7 @@
 	} else
 		for(iy = height; iy; iy--, i += step_up)
 			for(ix = 0; ix < width; ix++, i++) {
-				Bread(b, c, sizeof(c));
+				Breadn(b, c, sizeof(c));
 				buf[i].blue = (uchar)((c[0] << 3) & 0xf8);
 				buf[i].green = (uchar)(((((unsigned)c[1] << 6) +
 				                        (((unsigned)c[0]) >> 2))) & 0xf8);
@@ -406,7 +406,7 @@
 		for(iy = height; iy; iy--, i += step_up)
 			for(ix = 0; ix < width; ix++, i++) {
 				ulong val;
-				Bread(b, c, sizeof(c));
+				Breadn(b, c, sizeof(c));
 				val =  (ulong)c[0] + ((ulong)c[1] << 8) +
 				      ((ulong)c[2] << 16) + ((ulong)c[1] << 24);
 
@@ -427,7 +427,7 @@
 	} else
 		for(iy = height; iy; iy--, i += step_up)
 			for(ix = 0; ix < width; ix++, i++) {
-				Bread(b, c, nelem(c));
+				Breadn(b, c, nelem(c));
 				buf[i].blue = c[0];
 				buf[i].green = c[1];
 				buf[i].red = c[2];
--- a/sys/src/cmd/jpg/readgif.c
+++ b/sys/src/cmd/jpg/readgif.c
@@ -12,7 +12,6 @@
 	int		exten;
 };
 
-
 struct Header{
 	Biobuf	*fd;
 	char		err[256];
@@ -46,7 +45,7 @@
 static void			skipextension(Header*);
 static uchar*		readcmap(Header*, int);
 static uchar*		decode(Header*, Rawimage*, Entry*);
-static void			interlace(Header*, Rawimage*);
+static void		interlace(Header*, Rawimage*);
 
 static
 void
@@ -222,7 +221,7 @@
 void
 readheader(Header *h)
 {
-	if(Bread(h->fd, h->buf, 13) != 13)
+	if(Breadn(h->fd, h->buf, 13) != 13)
 		giferror(h, "ReadGIF: can't read header: %r");
 	memmove(h->vers, h->buf, 6);
 	if(strcmp(h->vers, "GIF87a")!=0 &&  strcmp(h->vers, "GIF89a")!=0)
@@ -247,7 +246,7 @@
 	if(size > 8)
 		giferror(h, "ReadGIF: can't handles %d bits per pixel", size);
 	size = 3*(1<<size);
-	if(Bread(h->fd, h->buf, size) != size)
+	if(Breadn(h->fd, h->buf, size) != size)
 		giferror(h, "ReadGIF: short read on color map");
 	map = malloc(size);
 	if(map == nil)
@@ -263,7 +262,7 @@
 	Rawimage *i;
 	int left, top, width, height;
 
-	if(Bread(h->fd, h->buf, 9) != 9)
+	if(Breadn(h->fd, h->buf, 9) != 9)
 		giferror(h, "ReadGIF: can't read image descriptor: %r");
 	i = malloc(sizeof(Rawimage));
 	if(i == nil)
@@ -295,7 +294,7 @@
 		giferror(h, "ReadGIF: can't read data: %r");
 	if(nbytes == 0)
 		return 0;
-	n = Bread(h->fd, data, nbytes);
+	n = Breadn(h->fd, data, nbytes);
 	if(n < 0)
 		giferror(h, "ReadGIF: can't read data: %r");
 	if(n != nbytes)
@@ -307,7 +306,7 @@
 void
 graphiccontrol(Header *h)
 {
-	if(Bread(h->fd, h->buf, 5+1) != 5+1)
+	if(Breadn(h->fd, h->buf, 5+1) != 5+1)
 		giferror(h, readerr);
 	h->flags = h->buf[1];
 	h->delay = h->buf[2]+(h->buf[3]<<8);
@@ -348,7 +347,7 @@
 	default:
 		giferror(h, "ReadGIF: unknown extension");
 	}
-	if(hsize>0 && Bread(h->fd, h->buf, hsize) != hsize)
+	if(hsize>0 && Breadn(h->fd, h->buf, hsize) != hsize)
 		giferror(h, extreaderr);
 	if(!hasdata){
 		/*
--- a/sys/src/cmd/jpg/readjpg.c
+++ b/sys/src/cmd/jpg/readjpg.c
@@ -448,7 +448,7 @@
 	case 0:
 		jpgerror(h, "ReadJPG: expecting marker; saw %.2x at offset %lld", m, Boffset(h->fd));
 	}
-	if(Bread(h->fd, tmp, 2) != 2)
+	if(Breadn(h->fd, tmp, 2) != 2)
     Readerr:
 		jpgerror(h, readerr);
 	n = int2(tmp, 0);
@@ -462,7 +462,7 @@
 		h->nbuf = n;
 	}
 	/* accept short reads to cope with some real-world jpegs */
-	if(Bread(h->fd, h->buf, n) < 0)
+	if(Breadn(h->fd, h->buf, n) < 0)
 		goto Readerr;
 	*markerp = m;
 	return n;
--- a/sys/src/cmd/jpg/readpng.c
+++ b/sys/src/cmd/jpg/readpng.c
@@ -100,7 +100,7 @@
 	ulong crc = 0, crc2;
 	int n, nr;
 
-	if(Bread(b, buf, 8) != 8)
+	if(Breadn(b, buf, 8) != 8)
 		return -1;
 	n = get4(buf);
 	memmove(type, buf+4, 4);
@@ -107,12 +107,12 @@
 	type[4] = 0;
 	if(n > m)
 		sysfatal("getchunk needed %d, had %d", n, m);
-	nr = Bread(b, d, n);
+	nr = Breadn(b, d, n);
 	if(nr != n)
 		sysfatal("getchunk read %d, expected %d", nr, n);
 	crc = blockcrc(crctab, crc, type, 4);
 	crc = blockcrc(crctab, crc, d, n);
-	if(Bread(b, buf, 4) != 4)
+	if(Breadn(b, buf, 4) != 4)
 		sysfatal("getchunk tlr failed");
 	crc2 = get4(buf);
 	if(crc != crc2)
@@ -389,7 +389,7 @@
 	ZlibW zw;
 
 	buf = pngmalloc(IDATSIZE, 0);
-	Bread(b, buf, sizeof PNGmagic);
+	Breadn(b, buf, sizeof PNGmagic);
 	if(memcmp(PNGmagic, buf, sizeof PNGmagic) != 0)
 		sysfatal("bad PNGmagic");
 
--- a/sys/src/cmd/jpg/readtga.c
+++ b/sys/src/cmd/jpg/readtga.c
@@ -113,7 +113,7 @@
 		free(h);
 		return nil;
 	}
-	if(Bread(bp, h->cmap, n) != n){
+	if(Breadn(bp, h->cmap, n) != n){
 		free(h);
 		free(h->cmap);
 		return nil;
@@ -124,7 +124,7 @@
 static int
 luma(Biobuf *bp, uchar *l, int num)
 {
-	return Bread(bp, l, num);
+	return Breadn(bp, l, num);
 }
 
 static int
@@ -164,7 +164,7 @@
 	switch(bpp){
 	case 16:
 		for(i = 0; i < num; i++){
-			if(Bread(bp, buf, 2) != 2)
+			if(Breadn(bp, buf, 2) != 2)
 				break;
 			x = buf[0];
 			y = buf[1];
@@ -175,7 +175,7 @@
 		break;
 	case 24:
 		for(i = 0; i < num; i++){
-			if(Bread(bp, buf, 3) != 3)
+			if(Breadn(bp, buf, 3) != 3)
 				break;
 			*b++ = buf[0];
 			*g++ = buf[1];
@@ -184,7 +184,7 @@
 		break;
 	case 32:
 		for(i = 0; i < num; i++){
-			if(Bread(bp, buf, 4) != 4)
+			if(Breadn(bp, buf, 4) != 4)
 				break;
 			*b++ = buf[0];
 			*g++ = buf[1];
--- a/sys/src/cmd/jpg/readv210.c
+++ b/sys/src/cmd/jpg/readv210.c
@@ -120,7 +120,7 @@
 		goto Error;
 
 	for(l = 0; l < lines; l++){
-		if(Bread(bp, buf, chunk) == -1)
+		if(Breadn(bp, buf, chunk) == -1)
 			goto Error;
 
 		rd = 0;
--- a/sys/src/cmd/jpg/readyuv.c
+++ b/sys/src/cmd/jpg/readyuv.c
@@ -126,7 +126,7 @@
 		goto Error;
 
 	for (l = 0; l < lines; l++) {
-		if (Bread(bp, buf, pixels *2) == -1)
+		if (Breadn(bp, buf, pixels *2) == -1)
 			goto Error;
 
 		base = l*pixels*2;
@@ -137,7 +137,7 @@
 
 	if (bits == 10)
 		for (l = 0; l < lines; l++) {
-			if (Bread(bp, buf, pixels / 2) == -1)
+			if (Breadn(bp, buf, pixels / 2) == -1)
 				goto Error;