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.
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | (* A growable byte buffer, its memory from an allocator. *) |
| 2 | 2 | module Buf; |
| 3 | -import Mem, Arena, Rt; | |
| 3 | +import Mem, Arena, Rt, Sys; | |
| 4 | 4 | |
| 5 | 5 | type |
| 6 | 6 | Buf* = record |
| @@ -23,6 +23,14 @@ | ||
| 23 | 23 | cap := b^.cap * 2; |
| 24 | 24 | if cap < 64 then cap := 64; end; |
| 25 | 25 | 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; | |
| 26 | 34 | p := Mem.alloc(b^.a, cap, 1); |
| 27 | 35 | if p = nil then return false; end; |
| 28 | 36 | if b^.len > 0 then COPY(p[0..b^.len], b^.data[0..b^.len]); end; |
An empty file.
| @@ -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. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -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 |