Skip to content

Commit

Permalink
Skip symbol insertion for unreachable basic blocks
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
fennecJ committed Sep 22, 2024
1 parent 3573b9f commit 7cd3787
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
Empty file added src/DU.h
Empty file.
2 changes: 2 additions & 0 deletions src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,8 @@ void bb_disconnect(basic_block_t *pred, basic_block_t *succ)
/* The symbol is an argument of function or the variable in declaration */
void add_symbol(basic_block_t *bb, var_t *var)
{
if (!bb)
return;
symbol_t *sym;
for (sym = bb->symbol_list.head; sym; sym = sym->next) {
if (sym->var == var)
Expand Down
9 changes: 9 additions & 0 deletions tests/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ int main() {
}
EOF

# Unreachable declaration should not cause prog seg-falut (prog should leave normally with exit code 0)
try_ 0 << EOF
int main()
{
return 0;
int a = 5;
}
EOF

try_ 1 << EOF
int is_odd(int x);
Expand Down

0 comments on commit 7cd3787

Please sign in to comment.