Skip to content

Commit feddce5

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 e57c5c6 commit feddce5

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
@@ -3578,12 +3578,14 @@ void read_ternary_operation(block_t *parent, basic_block_t **bb)
35783578
var_t *false_val = opstack_pop();
35793579
bool true_array = is_array_literal_placeholder(true_val);
35803580
bool false_array = is_array_literal_placeholder(false_val);
3581+
bool true_ptr_like = is_pointer_like_value(true_val);
3582+
bool false_ptr_like = is_pointer_like_value(false_val);
35813583

3582-
if (true_array && !false_array)
3584+
if (true_array && !false_ptr_like)
35833585
true_val = scalarize_array_literal(parent, &then_, true_val,
35843586
false_val ? false_val->type : NULL);
35853587

3586-
if (false_array && !true_array)
3588+
if (false_array && !true_ptr_like)
35873589
false_val = scalarize_array_literal(parent, &else_, false_val,
35883590
true_val ? true_val->type : NULL);
35893591

0 commit comments

Comments
 (0)