bradleyeckert / chad

A self-hosting Forth for J1-style CPUs
Other
24 stars 4 forks source link

Crash for definitions without reference numbers #3

Open uncle-betty opened 3 years ago

uncle-betty commented 3 years ago

I see the following crash on Ubuntu 18.04:

~/chad$ cd forth
~/chad/forth$ mkdir html
~/chad/forth$ gdb ../src/chad
GNU gdb (Ubuntu 8.1.1-0ubuntu1) 8.1.1
[...]
(gdb) run
Starting program: /home/tlo/work/symblif/chad/src/chad 
ok>include core.f

Program received signal SIGSEGV, Segmentation fault.
__strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
65      ../sysdeps/x86_64/multiarch/strlen-avx2.S: No such file or directory.
(gdb) bt
#0  __strlen_avx2 () at ../sysdeps/x86_64/multiarch/strlen-avx2.S:65
#1  0x00007ffff7a612e5 in __GI__IO_fputs (str=0x0, fp=0x5555559afcc0) at iofputs.c:33
#2  0x000055555555697d in LogR (s=0x0) at chad.c:580
#3  0x0000555555556d97 in LogColor (color=255, ID=258, s=0x5555557abf40 <tok> "cowait") at chad.c:678
#4  0x00005555555573ee in NotKeyword (key=0x5555557abf40 <tok> "cowait") at chad.c:798
#5  0x000055555555cb78 in chad (line=0x5555559ac400 <command> "   2drop  cowait", ' ' <repeats 24 times>, "\\ um* is a special case of fractional *", maxlength=128) at chad.c:2228
#6  0x000055555555f1ed in main (argc=1, argv=0x7fffffffe498) at main.c:21

After looking around in the code a little, it appears that this issue is that cowait's definition lacks a reference number. Hence, ...

I'm not sure what the correct fix would be. Should cowait have a reference number? Or should a Keyword without a .help text just be skipped by LogColor()?

uncle-betty commented 3 years ago

FWIW, in case it's OK to have definitions without reference numbers, the following changes would fix the issue, I think:

~/chad/src$ git diff -w
diff --git a/src/chad.c b/src/chad.c
index 6224539..28e2a95 100644
--- a/src/chad.c
+++ b/src/chad.c
@@ -671,11 +671,12 @@ SV LogColor(uint32_t color, int ID, char* s) {
         Log(itos(color, 16, 6, 1));
         LogR(">");
     }
-    if (ID) {
+    char* refStr = ID ? ReferenceString(ID) : NULL;
+    if (refStr) {
         LogR("<a href = \"");
         LogR(foundWidName);
         LogR(".html#");
-        LogR(ReferenceString(ID));
+        LogR(refStr);
         LogR("\" style=\"text-decoration: none; color: #");
         Log(itos(color, 16, 6, 1));
         LogR("\">");