From 9b4fd42f81f14c77a204a83486d9d8f077aebb3a Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 17:00:12 +0100 Subject: [PATCH] Buf: out of memory stops; Buf.spare to read into add and str dropped bytes silently when the allocator ran out, a corrupt buffer nobody hears of; now they stop with "Buf: out of memory", the calls under way, status 2, as a failed check does. room still asks first. Breaks no program whose memory suffices. spare(b, n) gives room for n more bytes and those bytes, for Sys.read to fill, then INC(b^.len, n). Files.read uses it: one copy of each byte, not two. It stops too when there's no room, not "", which a read would take for the end of the file. From PROPOSALS.md; test/run/BufFull, BufInit's trace through need. --- diff --git a/PROPOSALS.md b/PROPOSALS.md index 026df81..2f9a5c6 100644 --- a/PROPOSALS.md +++ b/PROPOSALS.md @@ -82,58 +82,6 @@ the budget; and, across modules, the imported module's text kept to be read again, which is to be checked. -## `Buf` when memory runs out - -`Buf.add` and `Buf.str` call `room`, and when it fails they drop the -bytes and return as if they had added them. The buffer is then short, -and nothing says so: in sauce a short buffer is an object written to -disk with bytes missing, a corrupt store. A program must call `room` -before every `add` to be safe, which no one does. - -The proposal: when `room` fails, `add` and `str` stop the program, as a -failed check does: "Buf: out of memory", the calls under way, status -2. `room` stays as it is, the way for a program that can do something -better to ask first. - -An `ERROR` result would be raz's usual way, but it would put a `try` or -an `_ :=` at every call, for a failure almost no program can deal with, -and it breaks every program that calls them as statements (a dropped -result, an error under `-Werror`): a thaw. Stopping breaks no program -whose memory suffices, and changes only what happens when it doesn't, -from silent damage to a stop, so it can come with any release. - -Met in porting sauce, which wraps every `Buf.str` in a `Buf.room` -(`Sbase.add`). - -## Reading into a `Buf` - -`Sys.read` fills a `[]U8` it is given. A `Buf`'s free room, -`data[len..cap]`, is its own, and `bytes` gives only `data[0..len]`, so -there is no slice of it to read into. A program that reads a file into -a `Buf` reads into an array on its stack and copies that in with -`Buf.str`: every byte is handled twice, and the stack holds a buffer as -big as each read. - -``` -proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, for a read to fill; "" if the allocator has none *) -``` - -The program reads into what `spare` gives and adds what it got to -`len`, which is already exported: - -``` -s := Buf.spare(ADR(b), 65536); -n := Sys.read(fd, s); -if n > 0 then INC(b.len, n); end; -``` - -The slice is good until the buffer next grows, as `bytes` is. It -breaks no program. `len` past `cap` is still the program's mistake to -make, as it is today; a `Buf.grow(b, n)` that checks it could stand -for the `INC`, if wanted. - -Met in sauce's `readFd`, which reads every file of the store this way. - ## A slice that outlives what it points at ``` diff --git a/doc/raz-lib.md b/doc/raz-lib.md index 2e2d35f..582937c 100644 --- a/doc/raz-lib.md +++ b/doc/raz-lib.md @@ -469,6 +469,7 @@ proc room*(b: ^Buf; n: INT): BOOL; (* room for n more bytes; false if the allocator has none *) proc add*(b: ^Buf; c: U8); proc str*(b: ^Buf; s: []U8); +proc spare*(b: ^Buf; n: INT): []U8; (* room for n more bytes, and those bytes, to fill *) proc bytes*(b: ^Buf): []U8; (* what it holds, until it next grows *) proc reset*(b: ^Buf); (* emptied, its memory kept *) proc done*(b: ^Buf); (* its memory given back *) @@ -532,7 +533,11 @@ ``` A `Buf`, a `List` and a `Map` take their memory from the allocator -`init` gives them. `Fmt`'s format takes the next argument at each +`init` gives them. `Buf.add`, `str` and `spare` stop the program, as a +failed check does, if the allocator has no room; `room` asks first. A +program reads into a `Buf` through `spare`, then adds what it read to +`len`: `s := Buf.spare(b, 4096); n := Sys.read(fd, s); if n > 0 then +INC(b^.len, n); end;`. `Fmt`'s format takes the next argument at each `{}`; `{{` and `}}` stand for `{` and `}`. A `Map` hashes and compares its keys with the procedures it is given: `import Count := Map([]U8, INT, Str.hash, Str.eq);`. diff --git a/lib/Buf.raz b/lib/Buf.raz index 30fdc2f..bef7023 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -40,24 +40,42 @@ return true; end; -(* Adds the byte c at the end; if room for it can't be had, it is - dropped and the buffer is as it was. *) -proc add*(b: ^Buf; c: U8); +(* Room for n more bytes, or the program stops: a buffer that went on + short of what was added to it would be wrong data, and nothing would + say so. A program that can do better asks room first. *) +proc need(b: ^Buf; n: INT); begin - if room(b, 1) then - b^.data[b^.len] := c; - INC(b^.len); + if not room(b, n) then + _ := Sys.write(Sys.stderr, "Buf: out of memory\n"); + Rt.trace; + Rt.exit(2); end; end; -(* Adds the bytes of s at the end; if room for them can't be had, none - is added. *) +(* Adds the byte c at the end. *) +proc add*(b: ^Buf; c: U8); +begin + need(b, 1); + b^.data[b^.len] := c; + INC(b^.len); +end; + +(* Adds the bytes of s at the end. *) proc str*(b: ^Buf; s: []U8); +begin + need(b, LEN(s)); + COPY(b^.data[b^.len..b^.len + LEN(s)], s); + INC(b^.len, LEN(s)); +end; + +(* Room for n more bytes, and those bytes, for a read to fill: the + program then adds to len what it filled. Good until the buffer next + grows, as bytes is. *) +proc spare*(b: ^Buf; n: INT): []U8; begin - if room(b, LEN(s)) then - COPY(b^.data[b^.len..b^.len + LEN(s)], s); - INC(b^.len, LEN(s)); - end; + if n = 0 then return ""; end; (* a new buffer has no data to slice *) + need(b, n); + return b^.data[b^.len..b^.len + n]; end; (* What the buffer holds, until it next grows. *) @@ -98,6 +116,10 @@ reset(ADR(b)); str(ADR(b), "again"); if bytes(ADR(b)) # "again" then Rt.stop("Buf.reset"); end; + s := spare(ADR(b), 3); + COPY(s, "abc"); + INC(b.len, 2); + if (LEN(s) # 3) or (bytes(ADR(b)) # "againab") then Rt.stop("Buf.spare"); end; done(ADR(b)); Arena.done(ADR(ar)); end; diff --git a/lib/Files.raz b/lib/Files.raz index 9b3b648..0b4ebc4 100644 --- a/lib/Files.raz +++ b/lib/Files.raz @@ -15,13 +15,13 @@ (* Adds the file at path to b; false if it can't be read. *) proc read*(path: []U8; b: ^Buf.Buf): BOOL; -var fd, n: INT; chunk: [4096]U8; +var fd, n: INT; begin fd := Sys.openRead(path); if fd < 0 then return false; end; repeat - n := Sys.read(fd, chunk); - if n > 0 then Buf.str(b, chunk[0..n]); end; + n := Sys.read(fd, Buf.spare(b, 65536)); (* straight into b *) + if n > 0 then INC(b^.len, n); end; until n <= 0; Sys.close(fd); return n = 0; diff --git a/test/run/BufFull.expect b/test/run/BufFull.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/BufFull.raz b/test/run/BufFull.raz new file mode 100644 index 0000000..7779b87 --- /dev/null +++ b/test/run/BufFull.raz @@ -0,0 +1,14 @@ +module BufFull; +(* a Buf whose allocator runs out stops the program, with the calls + under way, rather than dropping what it was given *) +import Buf, Arena, Rt; + +proc main*; +var ar: Arena.Arena; b: Buf.Buf; big: [10000]U8; +begin + if not Arena.init(ADR(ar), 4096) then Rt.stop("no arena"); end; + Buf.init(ADR(b), ADR(ar.base)); + Buf.str(ADR(b), big); +end; + +end BufFull. diff --git a/test/run/BufFull.status b/test/run/BufFull.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/BufFull.status @@ -0,0 +1 @@ +2 diff --git a/test/run/BufFull.stderr b/test/run/BufFull.stderr new file mode 100644 index 0000000..f790f70 --- /dev/null +++ b/test/run/BufFull.stderr @@ -0,0 +1,4 @@ +Buf: out of memory + in Buf.need at lib/Buf.raz:50:9: b = 0x?, n = 10000 + in Buf.str at lib/Buf.raz:66:5: b = 0x? + in BufFull.main at test/run/BufFull.raz:11:5 diff --git a/test/run/BufInit.stderr b/test/run/BufInit.stderr index 32ea17e..e121cd2 100644 --- a/test/run/BufInit.stderr +++ b/test/run/BufInit.stderr @@ -1,4 +1,5 @@ Buf used before Buf.init in Buf.room at lib/Buf.raz:31:9: b = 0x?, n = 5, cap = 64, p = nil - in Buf.str at lib/Buf.raz:57:5: b = 0x? + in Buf.need at lib/Buf.raz:48:5: b = 0x?, n = 5 + in Buf.str at lib/Buf.raz:66:5: b = 0x? in BufInit.main at test/run/BufInit.raz:9:5