shithub: gefs

Download patch

ref: e76fe27c3d86edf51dc6ccaade989fdd71ed44bf
parent: af37a8efb7c758e96c08ad684638087f8d1b6482
author: Michael Forney <mforney@mforney.org>
date: Thu Feb 3 05:54:08 EST 2022

fs: fix readb with missing block

If lookup returns Eexist, then there is no block for this part of
the file, and kv.v does not contain a block pointer.

Return early after zeroing the result in this case.

--- a/fs.c
+++ b/fs.c
@@ -254,6 +254,8 @@
 
 	fb = o & ~(Blksz-1);
 	fo = o & (Blksz-1);
+	if(fo+n > Blksz)
+		n = Blksz-fo;
 
 	k.k = buf;
 	k.nk = sizeof(buf);
@@ -262,21 +264,20 @@
 	PBIT64(k.k+9, fb);
 
 	e = lookup(f, &k, &kv, kvbuf, sizeof(kvbuf), 0);
-	if(e != nil && e != Eexist){
-		werrstr(e);
-		return -1;
+	if(e != nil){
+		if(e != Eexist){
+			werrstr(e);
+			return -1;
+		}
+		memset(d, 0, n);
+		return n;
 	}
 
 	bp = unpackbp(kv.v, kv.nv);
 	if((b = getblk(bp, GBraw)) == nil)
 		return -1;
-	if(fo+n > Blksz)
-		n = Blksz-fo;
-	if(b != nil){
-		memcpy(d, b->buf+fo, n);
-		putblk(b);
-	}else
-		memset(d, 0, n);
+	memcpy(d, b->buf+fo, n);
+	putblk(b);
 	return n;
 }