summaryrefslogtreecommitdiff
path: root/lib/bio
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-10-12 12:43:19 -0700
committerOri Bernstein <ori@eigenstate.org>2018-10-12 13:01:12 -0700
commit2a9a9eba576c0932b7828fda9995a2335e789b2f (patch)
tree4de8cca543e6cb4d9283e49ae1117e9f8e4294fe /lib/bio
parentb7eb902dc5451d0e74186bd65c5c90f87f785682 (diff)
downloadmc-2a9a9eba576c0932b7828fda9995a2335e789b2f.tar.gz
Actually read delimiter length bytes. Don't index out of bounds.
Diffstat (limited to 'lib/bio')
-rw-r--r--lib/bio/bio.myr49
1 files changed, 24 insertions, 25 deletions
diff --git a/lib/bio/bio.myr b/lib/bio/bio.myr
index 032a926..f5ce61b 100644
--- a/lib/bio/bio.myr
+++ b/lib/bio/bio.myr
@@ -413,37 +413,36 @@ const readdelim = {f, delim, drop
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
- ;;
- if ret.len > 0
- -> `std.Ok ret
- else
- -> `std.Err `Eof
+ /*
+ * 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
;;
| `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