Skip to content

fix: resolve segfault in typechecker_collect_decls when processing extern declarations (fixes #2)#34

Open
onurege3467 wants to merge 1 commit into
Raulgooo:mainfrom
onurege3467:fix/issue-2-typechecker-segfault-extern-decls
Open

fix: resolve segfault in typechecker_collect_decls when processing extern declarations (fixes #2)#34
onurege3467 wants to merge 1 commit into
Raulgooo:mainfrom
onurege3467:fix/issue-2-typechecker-segfault-extern-decls

Conversation

@onurege3467

Copy link
Copy Markdown

Summary

Fixes the segfault (SIGSEGV) in typechecker_collect_decls that blocked all typechecker tests. The function received a declarations list from callers but incorrectly accessed node->as.program.declarations, causing a misaligned pointer dereference (address 0x1) when extern declarations were present.

Root Cause

typechecker_collect_decls is called from two sites:

  • typechecker_check: passes program->as.program.declarations (declarations list)
  • typechecker_check_node for AST_BLOCK: passes node->as.block.statements (statements list)

In both cases, the parameter node is already the list head. But the function did node->as.program.declarations to initialize the loop variable, treating the list as a AST_PROGRAM node. When extern declarations are present (e.g., src/std/prelude.runes), this reads garbage memory and dereferences address 0x1.

Fix

Changed all 3 occurrences of node->as.program.declarations to node inside typechecker_collect_decls:

  1. Pass 1 (Types): AstNode *decl = node
  2. Pass 1.5 (Populate fields): decl = node
  3. Pass 2 (Functions, Externs, Variables): decl = node

Regression Test

Added src/tests/samples/99_extern_decl_regression.runes that exercises extern declaration typechecking with both an extern function and extern variable.

Verification

Before fix: ./runes src/std/prelude.runes segfaults (SIGSEGV, exit 139)
After fix: compiles successfully, all test suite samples pass

Closes #2

…tern declarations (fixes Raulgooo#2)

The typechecker_collect_decls function received a declarations list from its
callers (program->as.program.declarations or block->as.block.statements) but
incorrectly accessed node->as.program.declarations to get the first element.
When extern declarations were present (including in src/std/prelude.runes),
the misaligned pointer dereference caused a segfault (SIGSEGV).

Fix: change all 3 occurrences of node->as.program.declarations to node
(the parameter is already the declarations list).

Adds regression test 99_extern_decl_regression.runes that exercises extern
declaration typechecking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG-001: Fix segfault in typechecker_collect_decls (blocks all tests)

1 participant