From 0519c7d5db78634d864f767a246d3e7413d43dda Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Thu, 15 Oct 2026 16:46:50 +0100 Subject: [PATCH] Buf: a Buf used before Buf.init says so Before, it stopped as a nil pointer in Mem.alloc, three calls down. Now Buf.room says "Buf used before Buf.init", then the calls under way, and the program ends with status 2, as before. Met in porting sauce; test/run/BufInit. --- diff --git a/lib/Buf.raz b/lib/Buf.raz index 590ffb1..30fdc2f 100644 --- a/lib/Buf.raz +++ b/lib/Buf.raz @@ -1,6 +1,6 @@ (* A growable byte buffer, its memory from an allocator. *) module Buf; -import Mem, Arena, Rt; +import Mem, Arena, Rt, Sys; type Buf* = record @@ -23,6 +23,14 @@ cap := b^.cap * 2; if cap < 64 then cap := 64; end; while cap < b^.len + n do cap := cap * 2; end; + if b^.a = nil then + (* a zero Buf has no allocator: the core has no heap to assume, + so init must give it one. Said plainly, with the calls under + way, rather than as a nil pointer in Mem.alloc *) + _ := Sys.write(Sys.stderr, "Buf used before Buf.init\n"); + Rt.trace; + Rt.exit(2); + end; p := Mem.alloc(b^.a, cap, 1); if p = nil then return false; end; if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; diff --git a/test/run/BufInit.expect b/test/run/BufInit.expect new file mode 100644 index 0000000..e69de29 diff --git a/test/run/BufInit.raz b/test/run/BufInit.raz new file mode 100644 index 0000000..ef9e28d --- /dev/null +++ b/test/run/BufInit.raz @@ -0,0 +1,12 @@ +module BufInit; +(* a Buf used before Buf.init says so, with the calls under way, and + stops the program with status 2 *) +import Buf; + +proc main*; +var b: Buf.Buf; +begin + Buf.str(ADR(b), "hello"); +end; + +end BufInit. diff --git a/test/run/BufInit.status b/test/run/BufInit.status new file mode 100644 index 0000000..0cfbf08 --- /dev/null +++ b/test/run/BufInit.status @@ -0,0 +1 @@ +2 diff --git a/test/run/BufInit.stderr b/test/run/BufInit.stderr new file mode 100644 index 0000000..32ea17e --- /dev/null +++ b/test/run/BufInit.stderr @@ -0,0 +1,4 @@ +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 BufInit.main at test/run/BufInit.raz:9:5