bufio: readahead signals overflow when it cannot grow (F-B)

The scanner readahead silently fell through when start==0 and the
buffer was full at maxread, producing no bytes and no error. scanbyte
then spun forever re-requesting bytes that never came (catB-144) and
scanrune nil-dereferenced s.ptr[s.start] (catB-145).

Make readahead the single overflow choke-point: at the ceiling it
returns a bufio-local `overflow` before the grow, propagated through
scanbyte/scanrune/scanbytes (scanbytes drops its now-redundant manual
pre-check). Mirrors ref/hare/bufio/scanner.ha:174-182, which returns
errors::overflow there; ww uses bufio-local overflow because io.error
is a closed enum without an overflow member. Consumers (regex, the 778
embedded source) gain the totality arm.

Table-driven @test crosses {nil-ptr, zero-len} x {scanbyte, scanrune};
neutralizing the overflow return reproduces the catB-144 hang.
This commit is contained in:
2026-06-14 11:54:20 +09:00
parent 1752305be7
commit 851915e6fe
4 changed files with 125 additions and 41 deletions

View File

@@ -66,7 +66,7 @@ fn errsource() io.stream = {
let i: i32 = 0;
for (i < 6) {
let r: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
let r: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
match (r) {
case let b: u8 => {
assert(!(weof[i] != 0));
@@ -74,6 +74,7 @@ fn errsource() io.stream = {
};
case io.eof => { assert(!(weof[i] == 0)); };
case let e: io.error => abort();
case bufio.overflow => abort();
};
i += 1;
};
@@ -197,11 +198,12 @@ fn errsource() io.stream = {
let buf: [8]u8;
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
let r1: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
let r1: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
match (r1) {
case let b: u8 => abort();
case io.eof => { };
case let e: io.error => abort();
case bufio.overflow => abort();
};
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
@@ -230,11 +232,12 @@ fn errsource() io.stream = {
let buf: [8]u8;
let sc: bufio.scanner = bufio.newscannerbuf(m, buf[0:8]);
let r1: (u8 | io.eof | io.error) = bufio.scanbyte(&sc);
let r1: (u8 | io.eof | io.error | bufio.overflow) = bufio.scanbyte(&sc);
match (r1) {
case let b: u8 => abort();
case io.eof => abort();
case let e: io.error => { };
case bufio.overflow => abort();
};
let r2: (str | io.eof | io.error | bufio.overflow) = bufio.scanline(&sc);
@@ -703,7 +706,7 @@ fn errsource() io.stream = {
let i: i32 = 0;
for (i < 6) {
let r: (rune | io.eof | io.error | utf8.invalid) = bufio.scanrune(&sc);
let r: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc);
match (r) {
case let rn: rune => {
assert(!(weof[i] != 0));
@@ -712,6 +715,7 @@ fn errsource() io.stream = {
case io.eof => { assert(!(weof[i] == 0)); };
case let e: io.error => abort();
case utf8.invalid => abort();
case bufio.overflow => abort();
};
i += 1;
};
@@ -729,12 +733,13 @@ fn errsource() io.stream = {
let m1: io.stream = &mem1.vt;
let b1: [8]u8;
let sc1: bufio.scanner = bufio.newscannerbuf(m1, b1[0:8]);
let r1: (rune | io.eof | io.error | utf8.invalid) = bufio.scanrune(&sc1);
let r1: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc1);
match (r1) {
case let rn: rune => abort();
case io.eof => abort();
case let e: io.error => abort();
case utf8.invalid => { };
case bufio.overflow => abort();
};
bufio.finish(&sc1);
@@ -746,12 +751,13 @@ fn errsource() io.stream = {
let m2: io.stream = &mem2.vt;
let b2: [8]u8;
let sc2: bufio.scanner = bufio.newscannerbuf(m2, b2[0:8]);
let r2: (rune | io.eof | io.error | utf8.invalid) = bufio.scanrune(&sc2);
let r2: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc2);
match (r2) {
case let rn: rune => abort();
case io.eof => abort();
case let e: io.error => abort();
case utf8.invalid => { };
case bufio.overflow => abort();
};
bufio.finish(&sc2);
@@ -763,12 +769,13 @@ fn errsource() io.stream = {
let m3: io.stream = &mem3.vt;
let b3: [8]u8;
let sc3: bufio.scanner = bufio.newscannerbuf(m3, b3[0:8]);
let r3: (rune | io.eof | io.error | utf8.invalid) = bufio.scanrune(&sc3);
let r3: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc3);
match (r3) {
case let rn: rune => abort();
case io.eof => abort();
case let e: io.error => abort();
case utf8.invalid => { };
case bufio.overflow => abort();
};
bufio.finish(&sc3);
};
@@ -825,7 +832,7 @@ fn errsource() io.stream = {
i = 0;
for (i < 3) {
let r: (rune | io.eof | io.error | utf8.invalid) = bufio.scanrune(&sc2);
let r: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) = bufio.scanrune(&sc2);
match (r) {
case let rn: rune => {
assert(!(weof2[i] != 0));
@@ -834,6 +841,7 @@ fn errsource() io.stream = {
case io.eof => { assert(!(weof2[i] == 0)); };
case let e: io.error => abort();
case utf8.invalid => abort();
case bufio.overflow => abort();
};
i += 1;
};
@@ -859,3 +867,68 @@ fn errsource() io.stream = {
};
bufio.finish(&sc);
};
// ---- readahead can't-grow → overflow, not spin / nil-deref -----------
//
// drain F-B: a scanner that cannot buffer the next byte (zero-cap) makes
// readahead unable to make progress. Pre-fix it fell through silently to
// a zero-length io.read returning 0, so scanbyte spun forever (catB-144)
// and scanrune dereferenced the nil / zero-cap s.ptr[s.start]
// (catB-145). Both entry points must now surface bufio.overflow
// (ref/hare/bufio/scanner.ha:179-181). The table crosses the two
// zero-cap constructions — nil-ptr newscanner(,0) and zero-length
// newscannerbuf — with the two single-item entry points; every row is an
// overflow. Because the new code returns promptly, this test terminates
// where the pre-fix scanbyte path would hang.
@test fn readaheadcantgrow() void = {
let src: [4]u8;
src[0]=65u8; src[1]=66u8; src[2]=67u8; src[3]=68u8; // "ABCD"
// (ctor, entry): ctor 0=newscanner(,0) (nil ptr), 1=newscannerbuf
// (,[0:0]) (zero-len buffer); entry 0=scanbyte, 1=scanrune.
let ctor: [4]i32;
let entry: [4]i32;
ctor[0]=0; entry[0]=0; // newscanner + scanbyte
ctor[1]=0; entry[1]=1; // newscanner + scanrune
ctor[2]=1; entry[2]=0; // newscannerbuf + scanbyte
ctor[3]=1; entry[3]=1; // newscannerbuf + scanrune
let zbuf: [1]u8; // sliced to [0:0] for the zero-len construction
let i: i32 = 0;
for (i < 4) {
let mem: memio.stream = memio.fixed(src[0:4]);
let m: io.stream = &mem.vt;
let sc: bufio.scanner;
if (ctor[i] == 0) {
sc = bufio.newscanner(m, 0);
} else {
sc = bufio.newscannerbuf(m, zbuf[0:0]);
};
if (entry[i] == 0) {
let rb: (u8 | io.eof | io.error | bufio.overflow) =
bufio.scanbyte(&sc);
match (rb) {
case let b: u8 => abort();
case io.eof => abort();
case let e: io.error => abort();
case bufio.overflow => { };
};
} else {
let rr: (rune | io.eof | io.error | utf8.invalid | bufio.overflow) =
bufio.scanrune(&sc);
match (rr) {
case let rn: rune => abort();
case io.eof => abort();
case let e: io.error => abort();
case utf8.invalid => abort();
case bufio.overflow => { };
};
};
bufio.finish(&sc);
i += 1;
};
};