Skip to content

Commit bd68c7b

Browse files
committed
Fix pointer handling for array literals in ternary
The parser now checks whether the opposite branch is pointer-like before scalarizing an array literal in `src/parser.c` (lines 3579–3590). This keeps `(int[]){...}` branches pointer-valued when the ternary participates in pointer contexts (e.g., selecting between `int *` options), while still collapsing to a scalar in pure-scalar cases. Previously the literal branch was always forced to a scalar, causing `selected` to become `1` and leading to a segfault when dereferenced.
1 parent 1495cf8 commit bd68c7b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/parser.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3625,12 +3625,14 @@ void read_ternary_operation(block_t *parent, basic_block_t **bb)
36253625
var_t *false_val = opstack_pop();
36263626
bool true_array = is_array_literal_placeholder(true_val);
36273627
bool false_array = is_array_literal_placeholder(false_val);
3628+
bool true_ptr_like = is_pointer_like_value(true_val);
3629+
bool false_ptr_like = is_pointer_like_value(false_val);
36283630

3629-
if (true_array && !false_array)
3631+
if (true_array && !false_ptr_like)
36303632
true_val = scalarize_array_literal(parent, &then_, true_val,
36313633
false_val ? false_val->type : NULL);
36323634

3633-
if (false_array && !true_array)
3635+
if (false_array && !true_ptr_like)
36343636
false_val = scalarize_array_literal(parent, &else_, false_val,
36353637
true_val ? true_val->type : NULL);
36363638

0 commit comments

Comments
 (0)