shithub: MicroHs

Download patch

ref: 2372e6a96bc79214b69242a9d6a5f2755dec21d0
parent: 32c81bcbea042c1288fc66ba9e7b4dcf9e266a81
author: Lennart Augustsson <lennart.augustsson@epicgames.com>
date: Sat Aug 17 08:54:06 EDT 2024

Don't crash on C pointers when pretty printing.

--- a/src/runtime/eval.c
+++ b/src/runtime/eval.c
@@ -1876,9 +1876,11 @@
   putb('"', f);
 }
 
-/* Recursively print an expression.
-   This assumes that the shared nodes has been marked as such.
-*/
+/*
+ * Recursively print an expression.
+ * This assumes that the shared nodes has been marked as such.
+ * The prefix flag is used to get a readable dump.
+ */
 void
 printrec(BFILE *f, NODEPTR n, int prefix)
 {
@@ -1949,14 +1951,21 @@
     }
     break;
   case T_PTR:
-    if (PTR(n) == stdin)
-      putsb("IO.stdin", f);
-    else if (PTR(n) == stdout)
-      putsb("IO.stdout", f);
-    else if (PTR(n) == stderr)
-      putsb("IO.stderr", f);
-    else
-      ERR("Cannot serialize pointers");
+    if (prefix) {
+      putsb("PTR", f);
+    } else {
+#if 0
+      /* This doesn't work very well, since handles are allocated with malloc. */
+      if (PTR(n) == stdin)
+        putsb("IO.stdin", f);
+      else if (PTR(n) == stdout)
+        putsb("IO.stdout", f);
+      else if (PTR(n) == stderr)
+        putsb("IO.stderr", f);
+      else
+#endif
+        ERR("Cannot serialize pointers");
+    }
     break;
   case T_FUNPTR:
       ERR("Cannot serialize function pointers");
--