Skip to content

Commit 1a11b2a

Browse files
committed
Skip symbol insertion for unreachable basic blocks
Previously, the compiler would throw a segmentation fault if the compiled code contained a declaration after a return statement. For example: int main() { return 0; int a = 5; } This occurred when the basic block (bb) for unreachable code was null, while the function add_symbol(basic_block_t *bb, var_t *var) did not check for null before attempting to insert a variable, leading to a segmentation fault. This commit fixes the issue by skipping symbol insertion if the basic block is unreachable (null).
1 parent 3573b9f commit 1a11b2a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/globals.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,8 @@ void bb_disconnect(basic_block_t *pred, basic_block_t *succ)
524524
/* The symbol is an argument of function or the variable in declaration */
525525
void add_symbol(basic_block_t *bb, var_t *var)
526526
{
527+
if (!bb)
528+
return;
527529
symbol_t *sym;
528530
for (sym = bb->symbol_list.head; sym; sym = sym->next) {
529531
if (sym->var == var)

tests/driver.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ int main() {
271271
}
272272
EOF
273273

274+
# Unreachable declaration should not cause prog seg-falut (prog should leave normally with exit code 0)
275+
try_ 0 << EOF
276+
int main()
277+
{
278+
return 0;
279+
int a = 5;
280+
}
281+
EOF
282+
274283
try_ 1 << EOF
275284
int is_odd(int x);
276285

0 commit comments

Comments
 (0)