commit 0a92484ce7291fd32af9aab2e2b4a7399e57f65b
parent 549a5f48f3b58a04b3a7784d5c76ee303a9b8962
Author: Ryan Sepassi <rsepassi@gmail.com>
Date: Thu, 30 Apr 2026 09:55:31 -0700
cc: fix pp cond stack bug
Diffstat:
3 files changed, 28 insertions(+), 0 deletions(-)
diff --git a/cc/cc.scm b/cc/cc.scm
@@ -5109,6 +5109,7 @@
(pps-up-pending-set! st (deep-copy ctx (pps-up-pending st)))
(pps-out-buf-set! st (deep-copy ctx (pps-out-buf st)))
(pps-cur-file-set! st (deep-copy ctx (pps-cur-file st)))
+ (pps-cond-stack-set! st (deep-copy ctx (pps-cond-stack st)))
(pps-macros-set! st (deep-copy ctx (pps-macros st)))))
(define (parse-translation-unit ps)
diff --git a/tests/cc/134-decl-define-in-ifdef.c b/tests/cc/134-decl-define-in-ifdef.c
@@ -0,0 +1,26 @@
+/* Regression: decl boundary inside an open #ifdef block.
+ *
+ * Two top-level forms inside `#ifdef CCSCM ... #endif` — a decl
+ * followed by a #define — used to corrupt cc.scm's scratch heap. The
+ * #ifdef pushes onto pps-cond-stack; the typedef ends a top-level decl
+ * and triggers a scratch reset; promote-iter-buffers! deep-copies the
+ * surviving pp-state slots into the main heap before the reset, but
+ * was missing pps-cond-stack from that list. The cond-stack frame
+ * dangled, and the next #define's %pp-active? walk segfaulted.
+ *
+ * The fixture also stands in for the seven `<stdarg.h>`-using
+ * fixtures (015, 067, 076, 079, 097, 116, 131): all of them gate
+ * stdarg.h on `#ifndef CCSCM` and define va_list / va_start in the
+ * else branch — exactly the same dangling-cond-stack shape.
+ *
+ * Result: 0. */
+
+#ifdef CCSCM
+typedef char *T;
+#define M(x) (x)
+#else
+typedef char T;
+#define M(x) (x)
+#endif
+
+int main(void) { T a; a = 0; return M(0); }
diff --git a/tests/cc/134-decl-define-in-ifdef.expected-exit b/tests/cc/134-decl-define-in-ifdef.expected-exit
@@ -0,0 +1 @@
+0