raz / commit 0519c7d5

commit
0519c7d5db78634d864f767a246d3e7413d43dda
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b27fd91f
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.

patch · log

5 files changed, +26 -1

lib/Buf.raz (history) modified +9 -1

@@ -1,6 +1,6 @@
11 (* A growable byte buffer, its memory from an allocator. *)
22 module Buf;
3-import Mem, Arena, Rt;
3+import Mem, Arena, Rt, Sys;
44
55 type
66 Buf* = record
@@ -23,6 +23,14 @@
2323 cap := b^.cap * 2;
2424 if cap < 64 then cap := 64; end;
2525 while cap < b^.len + n do cap := cap * 2; end;
26+ if b^.a = nil then
27+ (* a zero Buf has no allocator: the core has no heap to assume,
28+ so init must give it one. Said plainly, with the calls under
29+ way, rather than as a nil pointer in Mem.alloc *)
30+ _ := Sys.write(Sys.stderr, "Buf used before Buf.init\n");
31+ Rt.trace;
32+ Rt.exit(2);
33+ end;
2634 p := Mem.alloc(b^.a, cap, 1);
2735 if p = nil then return false; end;
2836 if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end;

test/run/BufInit.expect (history) added +0 -0

An empty file.

test/run/BufInit.raz (history) added +12 -0

@@ -0,0 +1,12 @@
1+module BufInit;
2+(* a Buf used before Buf.init says so, with the calls under way, and
3+ stops the program with status 2 *)
4+import Buf;
5+
6+proc main*;
7+var b: Buf.Buf;
8+begin
9+ Buf.str(ADR(b), "hello");
10+end;
11+
12+end BufInit.

test/run/BufInit.status (history) added +1 -0

@@ -0,0 +1 @@
1+2

test/run/BufInit.stderr (history) added +4 -0

@@ -0,0 +1,4 @@
1+Buf used before Buf.init
2+ in Buf.room at lib/Buf.raz:31:9: b = 0x?, n = 5, cap = 64, p = nil
3+ in Buf.str at lib/Buf.raz:57:5: b = 0x?
4+ in BufInit.main at test/run/BufInit.raz:9:5