code wiki / _hdl_build / nx_emptylit_probe.nx
nx_emptylit_probe.nx source
↩ module page · 62 lines · 2881 B
1// nx_emptylit_probe.nx -- MEASURE the empty-string-literal defect instead of asserting its scope.
2// A grep found 3137 uses of `"" as *u8` in runtime/. Before filing that as an estate-wide landmine I have
3// to know WHICH shapes are actually broken, because "3137 sites are wrong" and "one shape is wrong" are
4// very different findings and only one of them is true.
5// license_tier: ORIGINAL expect_exit: 0
6import "nx_syscalls.nx"
7
8func w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
9func n_(v: i64) -> i64 {
10 let bb: *u8=sys_mmap(28); var m: i64=v
11 if m<0 { m=0-m; sys_write(1,"-" as *u8,1) }
12 let t: *u8=sys_mmap(28); var k: i64=0
13 if m==0 { t[0]=48 as u8; k=1 }
14 while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 }
15 var i: i64=0
16 while i<k { bb[i]=t[k-1-i]; i=i+1 }
17 sys_write(1,bb,k); return 0
18}
19func sl(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
20
21// SHAPE A: multi-branch function returning an empty literal first, a non-empty one after. This is the
22// shape that actually bit the ruler.
23func shape_a(i: i64) -> *u8 {
24 if i==0 { return "" as *u8 }
25 if i==1 { return "runtime/" as *u8 }
26 return "buildroot/" as *u8
27}
28// SHAPE B: same, but the empty literal is the LAST return
29func shape_b(i: i64) -> *u8 {
30 if i==1 { return "runtime/" as *u8 }
31 return "" as *u8
32}
33// SHAPE C: empty literal returned by a function with no other literal at all
34func shape_c() -> *u8 { return "" as *u8 }
35
36func main() -> i64 {
37 w("--- shape A: empty literal is the FIRST of several returns ---\n" as *u8)
38 w(" A(0) len=" as *u8); n_(sl(shape_a(0))); w(" bytes=[" as *u8); w(shape_a(0)); w("]\n" as *u8)
39 w(" A(1) len=" as *u8); n_(sl(shape_a(1))); w(" bytes=[" as *u8); w(shape_a(1)); w("]\n" as *u8)
40
41 w("--- shape B: empty literal is the LAST return ---\n" as *u8)
42 w(" B(0) len=" as *u8); n_(sl(shape_b(0))); w(" bytes=[" as *u8); w(shape_b(0)); w("]\n" as *u8)
43
44 w("--- shape C: empty literal is the ONLY literal in the function ---\n" as *u8)
45 w(" C() len=" as *u8); n_(sl(shape_c())); w(" bytes=[" as *u8); w(shape_c()); w("]\n" as *u8)
46
47 w("--- shape D: empty literal bound to a local, next to another literal ---\n" as *u8)
48 let e: *u8 = "" as *u8
49 let f: *u8 = "runtime/" as *u8
50 w(" D len=" as *u8); n_(sl(e)); w(" bytes=[" as *u8); w(e); w("] (f=[" as *u8); w(f); w("])\n" as *u8)
51
52 w("--- shape E: empty literal passed straight as an argument ---\n" as *u8)
53 w(" E len=" as *u8); n_(sl("" as *u8)); w(" bytes=[" as *u8); w("" as *u8); w("]\n" as *u8)
54
55 w("--- shape F: comparing an empty literal to itself ---\n" as *u8)
56 let g1: *u8 = "" as *u8
57 var same: i64 = 0
58 if g1[0]==(0 as u8) { same = 1 }
59 w(" F first byte is NUL = " as *u8); n_(same); w("\n" as *u8)
60 sys_exit(0)
61 return 0
62}