From 6e55e71bff7e7f294f0aa1602e5e7f2cd96f90b6 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:42 +0000 Subject: [PATCH 1/4] Fix pointer arithmetic on rows of arrays of an empty class in generated C [GH #555] GNU C gives an empty struct size zero, so the generated C does pointer arithmetic on pointers to such a struct on char *. This was not done for pointers to arrays of such structs (rows of a multidimensional array): the rows of "Z a[3][2]" all had the same address, and the difference of two row pointers did not compile. Scale subscripts, additions, subtractions, increments, decrements, and differences of such pointers by the size of the row. For a GNU variable-length row, the size is computed at run time from the variables holding its dimensions. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/c_gen_be.c | 246 +++++++++++++++--- ...gh-555-multidim-empty-class-array-rows.txt | 7 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../multidim-empty-class-array-rows.sft.cpp | 109 ++++++++ 5 files changed, 323 insertions(+), 41 deletions(-) create mode 100644 src/pending_changes/gh-555-multidim-empty-class-array-rows.txt create mode 100644 tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.2.txt create mode 100644 tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp diff --git a/src/c_gen_be.c b/src/c_gen_be.c index 849110531a..c9892eada9 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -5454,21 +5454,110 @@ type of the variable to which it refers. #endif /* !STANDALONE_C_GEN_BE */ #endif /* CHECKING */ +static a_type_ptr empty_struct_pointee(a_type_ptr type) +/* +If type is a pointer to a struct that has been generated as empty, or to an +array (of any rank, possibly variable-length, and of nonzero size) of such +structs, return the type pointed to. Otherwise, return NULL. The type +pointed to has size zero in the generated code, so pointer arithmetic on +such a pointer is done on char * and scaled by the number of structs in the +type pointed to (see dump_empty_struct_pointee_size). +*/ +{ + a_type_ptr pointee = NULL; + + if (is_pointer_type(type)) { + pointee = f_skip_typerefs(type_pointed_to(type)); + if (pointee->generated_as_empty_struct) { + /* A pointer to an empty struct. */ + } else if (is_array_type(pointee) && + f_skip_typerefs(underlying_array_element_type(pointee))-> + generated_as_empty_struct && + (is_vla_type(pointee) || pointee->size != 0)) { + /* A pointer to an array of empty structs. */ + } else { + pointee = NULL; + } /* if */ + } /* if */ + return pointee; +} /* empty_struct_pointee */ + + +static a_boolean empty_struct_pointee_size_is_one(a_type_ptr pointee) +/* +pointee is a type returned by empty_struct_pointee. Return TRUE if it +contains exactly one struct, i.e., if pointer arithmetic on char * need not +be scaled. +*/ +{ + return pointee->generated_as_empty_struct || + (!is_vla_type(pointee) && pointee->size == 1); +} /* empty_struct_pointee_size_is_one */ + + +static void dump_empty_struct_pointee_size(a_type_ptr pointee) +/* +pointee is a type returned by empty_struct_pointee. Put out the number of +structs it contains, by which pointer arithmetic on char * is scaled. For +a variable-length array, this is the product of the dimensions, where each +variable dimension is put out as in its array declarator (i.e., using the +variable holding the value of the dimension, if any), e.g., +"((long)__T1*3)" for an array of type "S[n][3]". +*/ +{ + if (pointee->generated_as_empty_struct) { + write_unsigned_num((a_host_large_unsigned)1); + } else if (!is_vla_type(pointee)) { + write_unsigned_num((a_host_large_unsigned)pointee->size); + } else { + a_type_ptr type = pointee; + a_targ_size_t constant_factor = 1; + write_tok_str("(("); + write_tok_str(int_kind_name_full(targ_ptrdiff_t_int_kind, + /*for_generated_code=*/TRUE)); + write_tok_ch(')'); + do { + if (type->variant.array.is_vla) { + a_vla_dimension_ptr vlap; + check_assertion(type->variant.array.has_assoc_vla_dimension); + vlap = find_vla_dimension(type); +#if DO_IL_LOWERING && !LOWER_VARIABLE_LENGTH_ARRAYS + if (vlap->dimension_variable != NULL) { + dump_variable_name(vlap->dimension_variable); + } else +#endif /* DO_IL_LOWERING && !LOWER_VARIABLE_LENGTH_ARRAYS */ + { + write_tok_ch('('); + dump_expression(vlap->dimension_expr); + write_tok_ch(')'); + } /* if */ + write_tok_ch('*'); + } else { + constant_factor *= type->variant.array.variant.number_of_elements; + } /* if */ + type = f_skip_typerefs(type->variant.array.element_type); + } while (is_array_type(type)); + write_unsigned_num((a_host_large_unsigned)constant_factor); + write_tok_ch(')'); + } /* if */ +} /* dump_empty_struct_pointee_size */ + + static a_boolean cast_to_ptr_to_empty_struct(a_type_ptr type, a_boolean *parens_needed) /* If the given type (the result type of an expression) is a pointer to a -struct that has been generated as empty, put out a cast to the specified -type and return TRUE. If *parens_needed is FALSE, precede the cast with -a left parenthesis and set *parens_needed to TRUE. This is used to -reverse the effect of casting operands of the expression to char * to -allow them to be used in pointer arithmetic in spite of the zero size. +struct that has been generated as empty (or to an array of such structs; +see empty_struct_pointee), put out a cast to the specified type and return +TRUE. If *parens_needed is FALSE, precede the cast with a left parenthesis +and set *parens_needed to TRUE. This is used to reverse the effect of +casting operands of the expression to char * to allow them to be used in +pointer arithmetic in spite of the zero size. */ { a_boolean did_cast = FALSE; - if (is_pointer_type(type) && - f_skip_typerefs(type_pointed_to(type))->generated_as_empty_struct) { + if (empty_struct_pointee(type) != NULL) { if (!*parens_needed) { /* We must supply parentheses to enclose the added cast. */ *parens_needed = TRUE; @@ -5485,14 +5574,12 @@ allow them to be used in pointer arithmetic in spite of the zero size. static void dump_lvalue_ptr_to_empty_struct(an_expr_node_ptr expr) /* expr is an lvalue node whose type is a pointer to a struct that has been -generated as empty. In order for this lvalue to be usable in pointer -arithmetic expressions, it must be cast to an lvalue pointer to char *, -which this function does. +generated as empty (see empty_struct_pointee). In order for this lvalue to +be usable in pointer arithmetic expressions, it must be cast to an lvalue +pointer to char *, which this function does. */ { - check_assertion(is_pointer_type(expr->type) && - f_skip_typerefs(type_pointed_to(expr->type))-> - generated_as_empty_struct && + check_assertion(empty_struct_pointee(expr->type) != NULL && expr->is_lvalue); write_tok_str("(*(char **)&"); dump_expr_with_parens(expr); @@ -5500,25 +5587,68 @@ which this function does. } /* dump_lvalue_ptr_to_empty_struct */ -static void dump_possible_ptr_to_empty_struct(an_expr_node_ptr expr) +static void dump_possible_ptr_to_empty_struct(an_expr_node_ptr expr, + a_type_ptr pointee) /* expr is an operand of a pointer arithmetic operation (it need not be the pointer operand). If it is a pointer to struct that has been generated as -empty, cast it to char * to give it the right element size for the -computation. Otherwise, just dump the expression normally. +empty (see empty_struct_pointee), cast it to char * to give it the right +element size for the computation. Otherwise, if pointee (the value of +empty_struct_pointee for the pointer operand) is non-NULL, expr is the +integer operand, so scale it by the size of pointee if necessary. +Otherwise, just dump the expression normally. */ { - if (is_pointer_type(expr->type) && - f_skip_typerefs(type_pointed_to(expr->type))-> - generated_as_empty_struct) { + if (empty_struct_pointee(expr->type) != NULL) { write_tok_str("((char *)"); dump_expr_with_parens(expr); write_tok_ch(')'); + } else if (pointee != NULL && !empty_struct_pointee_size_is_one(pointee)) { + write_tok_str("(("); + dump_expression(expr); + write_tok_str(")*"); + dump_empty_struct_pointee_size(pointee); + write_tok_ch(')'); } else { dump_expr_with_parens(expr); } /* if */ } /* dump_possible_ptr_to_empty_struct */ + +static void dump_incr_decr_of_ptr_to_empty_struct(an_expr_node_ptr operand, + a_boolean is_incr, + a_boolean is_prefix) +/* +Dump an increment (if is_incr) or decrement of operand, a pointer to struct +that has been generated as empty (see empty_struct_pointee), as a prefix +(if is_prefix) or postfix operation on operand cast to an lvalue of type +char * (see dump_lvalue_ptr_to_empty_struct). When the size of the type +pointed to is not 1, use "(p += size)" for "++p" and "((p += size) - size)" +for "p++" (and similarly for decrements). The caller casts the result back +to the type of operand (see cast_to_ptr_to_empty_struct). +*/ +{ + a_type_ptr pointee = empty_struct_pointee(operand->type); + + if (empty_struct_pointee_size_is_one(pointee)) { + if (is_prefix) write_tok_str(is_incr ? "++" : "--"); + dump_lvalue_ptr_to_empty_struct(operand); + if (!is_prefix) write_tok_str(is_incr ? "++" : "--"); + } else { + write_tok_ch('('); + if (!is_prefix) write_tok_ch('('); + dump_lvalue_ptr_to_empty_struct(operand); + write_tok_str(is_incr ? " += " : " -= "); + dump_empty_struct_pointee_size(pointee); + write_tok_ch(')'); + if (!is_prefix) { + write_tok_str(is_incr ? " - " : " + "); + dump_empty_struct_pointee_size(pointee); + write_tok_ch(')'); + } /* if */ + } /* if */ +} /* dump_incr_decr_of_ptr_to_empty_struct */ + #if SUNPRO_C_IS_C_GEN_BE_TARGET static an_expr_node_ptr skip_comma_nodes(an_expr_node_ptr opnd) @@ -5783,6 +5913,8 @@ there's some possibility of precedence confusion and need_parens is TRUE. #endif /* !C_GEN_BE_GENERATES_ANSI_C */ a_boolean ptr_to_empty_struct_case = FALSE; a_boolean pointer_arithmetic_op = FALSE; + a_type_ptr empty_struct_pointee_type = NULL; + a_boolean scaled_pdiff = FALSE; an_expr_node_ptr ptr_operand; a_boolean comma_case; @@ -6029,13 +6161,13 @@ there's some possibility of precedence confusion and need_parens is TRUE. ptr_to_empty_struct_case = cast_to_ptr_to_empty_struct(expr->type, &need_parens); if (ptr_to_empty_struct_case) { - dump_lvalue_ptr_to_empty_struct(operand_1); + dump_incr_decr_of_ptr_to_empty_struct(operand_1, + /*is_incr=*/TRUE, + /*is_prefix=*/FALSE); + write_tok_ch(')'); } else { dump_expr_with_parens(operand_1); - } /* if */ - write_tok_str("++"); - if (ptr_to_empty_struct_case) { - write_tok_ch(')'); + write_tok_str("++"); } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C end_adjust_bit_field_value(expr); @@ -6050,11 +6182,13 @@ there's some possibility of precedence confusion and need_parens is TRUE. #endif /* !C_GEN_BE_GENERATES_ANSI_C */ ptr_to_empty_struct_case = cast_to_ptr_to_empty_struct(expr->type, &need_parens); - write_tok_str("++"); if (ptr_to_empty_struct_case) { - dump_lvalue_ptr_to_empty_struct(operand_1); + dump_incr_decr_of_ptr_to_empty_struct(operand_1, + /*is_incr=*/TRUE, + /*is_prefix=*/TRUE); write_tok_ch(')'); } else { + write_tok_str("++"); dump_expr_with_parens(operand_1); } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C @@ -6071,13 +6205,13 @@ there's some possibility of precedence confusion and need_parens is TRUE. ptr_to_empty_struct_case = cast_to_ptr_to_empty_struct(expr->type, &need_parens); if (ptr_to_empty_struct_case) { - dump_lvalue_ptr_to_empty_struct(operand_1); + dump_incr_decr_of_ptr_to_empty_struct(operand_1, + /*is_incr=*/FALSE, + /*is_prefix=*/FALSE); + write_tok_ch(')'); } else { dump_expr_with_parens(operand_1); - } /* if */ - write_tok_str("--"); - if (ptr_to_empty_struct_case) { - write_tok_ch(')'); + write_tok_str("--"); } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C end_adjust_bit_field_value(expr); @@ -6092,11 +6226,13 @@ there's some possibility of precedence confusion and need_parens is TRUE. #endif /* !C_GEN_BE_GENERATES_ANSI_C */ ptr_to_empty_struct_case = cast_to_ptr_to_empty_struct(expr->type, &need_parens); - write_tok_str("--"); if (ptr_to_empty_struct_case) { - dump_lvalue_ptr_to_empty_struct(operand_1); + dump_incr_decr_of_ptr_to_empty_struct(operand_1, + /*is_incr=*/FALSE, + /*is_prefix=*/TRUE); write_tok_ch(')'); } else { + write_tok_str("--"); dump_expr_with_parens(operand_1); } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C @@ -6302,9 +6438,13 @@ there's some possibility of precedence confusion and need_parens is TRUE. write_tok_str("(0,"); } /* if */ #endif /* !C_GEN_BE_GENERATES_ANSI_C */ - dump_expr_with_parens(operand_2); if (ptr_to_empty_struct_case) { + dump_possible_ptr_to_empty_struct( + operand_2, + empty_struct_pointee(operand_1->type)); write_tok_ch(')'); + } else { + dump_expr_with_parens(operand_2); } /* if */ #if !C_GEN_BE_GENERATES_ANSI_C if (remainder_special_case) write_tok_ch(')'); @@ -6380,15 +6520,17 @@ there's some possibility of precedence confusion and need_parens is TRUE. case eok_subscript: ptr_operand = subscript_or_padd_pointer_operand(expr); check_assertion(is_pointer_type(ptr_operand->type)); - if (f_skip_typerefs(type_pointed_to(ptr_operand->type))-> - generated_as_empty_struct) { + if (empty_struct_pointee(ptr_operand->type) != NULL) { /* In order to do the pointer arithmetic required for the subscripting operation, the pointer must be cast to char * to get the appropriate size for the elements, and the result must be cast back to the appropriate element type. It's easier to deal with the casts in the pointer-addition form, so we use that instead of the subscript notation: if p is a - pointer to S, p[i] becomes (*(S *)(((char *)p)+i)). */ + pointer to S, p[i] becomes (*(S *)(((char *)p)+i)). (For a + pointer to an array of such structs, i is scaled by the size + of the array; see empty_struct_pointee.) */ + a_type_ptr pointee = empty_struct_pointee(ptr_operand->type); if (!need_parens) { write_tok_ch('('); need_parens = TRUE; @@ -6396,9 +6538,9 @@ there's some possibility of precedence confusion and need_parens is TRUE. write_tok_ch('*'); dump_cast(ptr_operand->type); write_tok_ch('('); - dump_possible_ptr_to_empty_struct(operand_1); + dump_possible_ptr_to_empty_struct(operand_1, pointee); write_tok_str(" + "); - dump_possible_ptr_to_empty_struct(operand_2); + dump_possible_ptr_to_empty_struct(operand_2, pointee); write_tok_ch(')'); } else { dump_expr_with_parens(operand_1); @@ -6808,6 +6950,22 @@ there's some possibility of precedence confusion and need_parens is TRUE. if (pointer_arithmetic_op) { ptr_to_empty_struct_case = cast_to_ptr_to_empty_struct(expr->type, &need_parens); + empty_struct_pointee_type = empty_struct_pointee(operand_1->type); + if (empty_struct_pointee_type == NULL && !is_unary) { + empty_struct_pointee_type = empty_struct_pointee(operand_2->type); + } /* if */ + if (op == (an_expr_operator_kind)eok_pdiff && + empty_struct_pointee_type != NULL && + !empty_struct_pointee_size_is_one(empty_struct_pointee_type)) { + /* The difference of the char * operands is in bytes, so divide + it by the size of the type pointed to. */ + if (!need_parens) { + write_tok_ch('('); + need_parens = TRUE; + } /* if */ + write_tok_ch('('); + scaled_pdiff = TRUE; + } /* if */ } /* if */ if (is_unary) { /* Unary operator; operator goes first. */ @@ -6821,7 +6979,8 @@ there's some possibility of precedence confusion and need_parens is TRUE. comma_column = curr_output_column; } /* if */ if (pointer_arithmetic_op) { - dump_possible_ptr_to_empty_struct(operand_1); + dump_possible_ptr_to_empty_struct(operand_1, + empty_struct_pointee_type); } else { dump_expr_with_parens(operand_1); } /* if */ @@ -6839,11 +6998,16 @@ there's some possibility of precedence confusion and need_parens is TRUE. } /* if */ if (pointer_comparison) write_tok_str(pointer_comparison_cast); if (pointer_arithmetic_op) { - dump_possible_ptr_to_empty_struct(operand_2); + dump_possible_ptr_to_empty_struct(operand_2, + empty_struct_pointee_type); } else { dump_expr_with_parens(operand_2); } /* if */ } /* if */ + if (scaled_pdiff) { + write_tok_str(") / "); + dump_empty_struct_pointee_size(empty_struct_pointee_type); + } /* if */ if (ptr_to_empty_struct_case) { write_tok_ch(')'); } /* if */ diff --git a/src/pending_changes/gh-555-multidim-empty-class-array-rows.txt b/src/pending_changes/gh-555-multidim-empty-class-array-rows.txt new file mode 100644 index 0000000000..f4a7fa830e --- /dev/null +++ b/src/pending_changes/gh-555-multidim-empty-class-array-rows.txt @@ -0,0 +1,7 @@ +[GH #555] +Rows of multidimensional arrays of an empty class in generated C + +When the C-generating back end used an empty struct for an empty class, the +rows of a multidimensional array of that class (e.g., "Z a[3][2]") all had the +same address, and the difference of two pointers to rows did not compile. +Pointer arithmetic on pointers to such rows is now scaled by the row size. diff --git a/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.1.txt b/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.1.txt new file mode 100644 index 0000000000..ad24f9777a --- /dev/null +++ b/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ Test_name.c diff --git a/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.2.txt b/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.multidim-empty-class-array-rows.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp b/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp new file mode 100644 index 0000000000..2e3dc96cb3 --- /dev/null +++ b/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp @@ -0,0 +1,109 @@ +//type:rp +//options_all:--c++11 --g++ + +// Row indexing and pointer arithmetic on multidimensional arrays of an +// empty class. When the generated C uses an empty struct for the class +// (size zero in GNU C), pointers to its rows also point to a type of size +// zero in the generated C, so pointer arithmetic on them must be scaled by +// the size of the row. Before the fix, this was done only for pointers to +// the class itself: the rows of "Z a[3][2]" all had the same address, and +// the difference of two row pointers did not compile. Rows that are GNU +// variable-length arrays are scaled by their run-time size. + +int errors; + +struct Z {}; +static Z *addrs[16]; +static int nctors; +struct C { + C() { addrs[nctors++] = (Z *)this; } +}; + +#define CHECK(cond) do { if (!(cond)) ++errors; } while (0) + +static long off(const void *p, const void *base) { + return (const char *)p - (const char *)base; +} + +static void vla(int n) { + Z a[3][n]; + Z (*p)[n] = a; + Z (*q)[n] = p + 1; + CHECK(off(&a[1][0], a) == n); + CHECK(off(&a[2][1], a) == 2 * n + 1); + CHECK(off(a[2], a) == 2 * n); + CHECK(off(q, a) == n); + CHECK(off(&q[1], a) == 2 * n); + CHECK(q - p == 1); + CHECK(&a[2] - p == 2); + CHECK(off(++p, a) == n); + CHECK(off(p++, a) == n); + CHECK(off(p, a) == 2 * n); + CHECK(off(p--, a) == 2 * n); + CHECK(off(--p, a) == 0); + p += 2; + CHECK(off(p, a) == 2 * n); + p -= 1; + CHECK(off(p, a) == n); + + Z b[2][n][3]; + Z (*r)[n][3] = b; + CHECK(off(&b[1][1][2], b) == 3 * n + 5); + CHECK(off(b[1][1], b) == 3 * n + 3); + CHECK(off(r + 1, b) == 3 * n); + CHECK((r + 1) - r == 1); + Z (*s)[3] = b[1]; + CHECK(off(s + 1, b) == 3 * n + 3); + + nctors = 0; + C c[2][n]; + CHECK(nctors == 2 * n); + for (int k = 0; k < 2 * n; ++k) CHECK(off(addrs[k], c) == k); +} + +volatile int two = 2, three = 3; + +int main() { + vla(two); + vla(three); + Z a[3][2]; + Z (*p)[2] = a; + Z (*q)[2] = p + 1; + CHECK(off(&a[1][0], a) == 2); + CHECK(off(&a[2][1], a) == 5); + CHECK(off(a[2], a) == 4); + CHECK(off(q, a) == 2); + CHECK(off(&q[1], a) == 4); + CHECK(off(&1[q], a) == 4); + CHECK(off(q - 1, a) == 0); + CHECK(q - p == 1); + CHECK(&a[2] - p == 2); + CHECK(off(++p, a) == 2); + CHECK(off(p++, a) == 2); + CHECK(off(p, a) == 4); + CHECK(off(p--, a) == 4); + CHECK(off(--p, a) == 0); + p += 2; + CHECK(off(p, a) == 4); + p -= 1; + CHECK(off(p, a) == 2); + int i = 2; + CHECK(off(p + i - 2, a) == 2); + CHECK(off(&a[i][1], a) == 5); + + Z b[2][2][3]; + Z (*r)[2][3] = b; + CHECK(off(&b[1][1][2], b) == 11); + CHECK(off(b[1][1], b) == 9); + CHECK(off(r + 1, b) == 6); + CHECK((r + 1) - r == 1); + Z (*s)[3] = b[1]; + CHECK(off(s + 1, b) == 9); + CHECK(s + 1 - b[0] == 3); + + /* Each element of a 2-D array of a class constructed at its address. */ + nctors = 0; + C c[3][2] = {}; + for (int k = 0; k < 6; ++k) CHECK(off(addrs[k], c) == k); + return errors != 0; +} From d7c4b070856edbb51a0963ef9275d8e94bc97bb1 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:42:28 +0000 Subject: [PATCH 2/4] Drop a redundant enumerator cast [GH #555] Following review feedback on a related pull request, compare the operator kind with eok_pdiff directly; the cast is redundant in C++. No functional change. --- src/c_gen_be.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/c_gen_be.c b/src/c_gen_be.c index c9892eada9..7e1ee3f9ad 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -6954,7 +6954,7 @@ there's some possibility of precedence confusion and need_parens is TRUE. if (empty_struct_pointee_type == NULL && !is_unary) { empty_struct_pointee_type = empty_struct_pointee(operand_2->type); } /* if */ - if (op == (an_expr_operator_kind)eok_pdiff && + if (op == eok_pdiff && empty_struct_pointee_type != NULL && !empty_struct_pointee_size_is_one(empty_struct_pointee_type)) { /* The difference of the char * operands is in bytes, so divide From 49f5875f75e3fb8c6b3d83d04abd0100942486bf Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 14:12:19 +0000 Subject: [PATCH 3/4] Keep the first argument on the call line [GH #555] Following review feedback on a related pull request, start the argument list of the dump_possible_ptr_to_empty_struct call on the line of the call itself. No functional change. --- src/c_gen_be.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/c_gen_be.c b/src/c_gen_be.c index 7e1ee3f9ad..adb2e1d385 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -6439,8 +6439,7 @@ there's some possibility of precedence confusion and need_parens is TRUE. } /* if */ #endif /* !C_GEN_BE_GENERATES_ANSI_C */ if (ptr_to_empty_struct_case) { - dump_possible_ptr_to_empty_struct( - operand_2, + dump_possible_ptr_to_empty_struct(operand_2, empty_struct_pointee(operand_1->type)); write_tok_ch(')'); } else { From df06f0eb241ef17af54ccab0821b80e77073dd62 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 16:35:07 +0000 Subject: [PATCH 4/4] Scale row pointer arithmetic by a signed size [GH #555] The size by which pointer arithmetic on rows of an empty class is scaled was cast to ptrdiff_t only before its first variable dimension, so a later dimension of type unsigned long made it unsigned and the difference of two row pointers came out as a large positive value. A constant size was put out as a plain int literal, so the scaled index was computed in int and could overflow. Cast each variable dimension, and a constant size, to ptrdiff_t. Reported by CodeRabbit. The regression test now also covers rows with two variable dimensions (one of type unsigned long) and negative differences. --- src/c_gen_be.c | 20 ++++++++++++++----- .../multidim-empty-class-array-rows.sft.cpp | 15 ++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/c_gen_be.c b/src/c_gen_be.c index adb2e1d385..b7eab963a6 100644 --- a/src/c_gen_be.c +++ b/src/c_gen_be.c @@ -5502,25 +5502,35 @@ structs it contains, by which pointer arithmetic on char * is scaled. For a variable-length array, this is the product of the dimensions, where each variable dimension is put out as in its array declarator (i.e., using the variable holding the value of the dimension, if any), e.g., -"((long)__T1*3)" for an array of type "S[n][3]". +"((long)__T1*(long)__T2*3)" for an array of type "S[n][m][3]". Except +for the size 1, the number has the (signed) type ptrdiff_t, so that the +scaling neither overflows a narrower integer operand nor makes a pointer +difference unsigned. */ { + const char *ptrdiff_name = int_kind_name_full(targ_ptrdiff_t_int_kind, + /*for_generated_code=*/TRUE); + if (pointee->generated_as_empty_struct) { write_unsigned_num((a_host_large_unsigned)1); } else if (!is_vla_type(pointee)) { + write_tok_str("(("); + write_tok_str(ptrdiff_name); + write_tok_ch(')'); write_unsigned_num((a_host_large_unsigned)pointee->size); + write_tok_ch(')'); } else { a_type_ptr type = pointee; a_targ_size_t constant_factor = 1; - write_tok_str("(("); - write_tok_str(int_kind_name_full(targ_ptrdiff_t_int_kind, - /*for_generated_code=*/TRUE)); - write_tok_ch(')'); + write_tok_ch('('); do { if (type->variant.array.is_vla) { a_vla_dimension_ptr vlap; check_assertion(type->variant.array.has_assoc_vla_dimension); vlap = find_vla_dimension(type); + write_tok_ch('('); + write_tok_str(ptrdiff_name); + write_tok_ch(')'); #if DO_IL_LOWERING && !LOWER_VARIABLE_LENGTH_ARRAYS if (vlap->dimension_variable != NULL) { dump_variable_name(vlap->dimension_variable); diff --git a/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp b/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp index 2e3dc96cb3..9c7c0fbb0d 100644 --- a/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp +++ b/tests/tests/regressions/multidim-empty-class-array-rows.sft.cpp @@ -52,9 +52,24 @@ static void vla(int n) { CHECK(off(b[1][1], b) == 3 * n + 3); CHECK(off(r + 1, b) == 3 * n); CHECK((r + 1) - r == 1); + CHECK(r - (r + 1) == -1); Z (*s)[3] = b[1]; CHECK(off(s + 1, b) == 3 * n + 3); + // Rows with more than one variable dimension, including one of unsigned + // type: the row size, and so the difference, must still be signed. + unsigned long m = n + 1; + Z d[3][n][m]; + Z (*t)[n][m] = d; + CHECK(off(t + 2, d) == 2 * n * (long)m); + CHECK(off(&t[2][1][1], d) == (2 * n + 1) * (long)m + 1); + CHECK(t - (t + 2) == -2); + CHECK(&d[0] - &d[2] == -2); + Z e[3][n][n]; + Z (*u)[n][n] = e + 2; + CHECK(off(u - 1, e) == n * n); + CHECK(e - u == -2); + nctors = 0; C c[2][n]; CHECK(nctors == 2 * n);