shithub: mc

Download patch

ref: 2a9a9eba576c0932b7828fda9995a2335e789b2f
parent: b7eb902dc5451d0e74186bd65c5c90f87f785682
author: Ori Bernstein <ori@eigenstate.org>
date: Fri Oct 12 08:43:19 EDT 2018

Actually read delimiter length bytes. Don't index out of bounds.

--- a/lib/bio/bio.myr
+++ b/lib/bio/bio.myr
@@ -413,37 +413,36 @@
 
 	ret = [][:]
 	while true
-		/* get at least delimiter count of characters */
-		match ensureread(f, 1)
+		match ensureread(f, delim.len)
 		| `std.Err `Eof:
-			if !drop
-				readinto(f, &ret, f.rend - f.rstart)
-			else
-				f.rstart += f.rend - f.rstart
+			/* 
+			 * We tried to get a delimiter, but came short.
+			 * Return the whole end of the buffer as the
+			 * read value, as though there was a terminating
+			 * delimiter at the end. If we're at 0 bytes even
+			 * then, return an eof.
+			 */
+			readinto(f, &ret, f.rend - f.rstart)
+			match ret.len
+			| 0:	-> `std.Err `Eof
+			| _:	-> `std.Ok ret
 			;;
-			if ret.len > 0
-				-> `std.Ok ret
-			else
-				-> `std.Err `Eof
-			;;
 		| `std.Err e:	-> `std.Err e
-		| `std.Ok _:
+		| `std.Ok _:	/* nothing: scan the buffer */
 		;;
-		for var i = f.rstart; i < f.rend; i++
-			if f.rbuf[i] == delim[0]
-				for var j = 0; j < delim.len; j++
-					if f.rbuf[i + j] != delim[j]
-						goto nextiterread
-					;;
+		for var i = f.rstart; i < f.rend - delim.len; i++
+			for var j = 0; j < delim.len; j++
+				if f.rbuf[i + j] != delim[j]
+					goto nextiterread
 				;;
-				if !drop
-					readinto(f, &ret, i - f.rstart)
-				else
-					f.rstart += i - f.rstart
-				;;
-				f.rstart += delim.len
-				-> `std.Ok ret
 			;;
+			if !drop
+				readinto(f, &ret, i - f.rstart)
+			else
+				f.rstart += i - f.rstart
+			;;
+			f.rstart += delim.len
+			-> `std.Ok ret
 :nextiterread
 		;;
 		if !drop