authorgravatar for djpohly@gmail.comDevin J. Pohly <djpohly@gmail.com> 2024-06-13 23:49:29-05:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2024-06-15 18:19:33+02:00
logffb1a6d9a75a7c8804c0e9db0e23ac54265f447c
treeb305e12bbf1ebbb64e1f9b3289e648c78404da8e
parent7829be6ee07aba6574fa5035c3d33023b3e23de7

translate-c: fix translation of "ptr += uint"

The right-hand side was incorrectly cast to a pointer, since only signed ints were being interpreted correctly as pointer arithmetic. Fixes #20285.

2 files changed, 23 insertions(+), 1 deletions(-)

src/translate_c.zig+2-1
...@@ -3824,8 +3824,9 @@ fn transCreateCompoundAssign(...@@ -3824,8 +3824,9 @@ fn transCreateCompoundAssign(
3824 const lhs_qt = getExprQualType(c, lhs);3824 const lhs_qt = getExprQualType(c, lhs);
3825 const rhs_qt = getExprQualType(c, rhs);3825 const rhs_qt = getExprQualType(c, rhs);
3826 const is_signed = cIsSignedInteger(lhs_qt);3826 const is_signed = cIsSignedInteger(lhs_qt);
3827 const is_ptr_arithmetic = qualTypeIsPtr(lhs_qt) and cIsInteger(rhs_qt);
3827 const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);3828 const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);
3828 const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed;3829 const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_arithmetic;
38293830
3830 if (used == .unused) {3831 if (used == .unused) {
3831 // common case3832 // common case
test/cases/run_translated_c/compound_assignments_with_pointer_arithmetic.c created+21
...@@ -0,0 +1,21 @@
1int main() {
2 const char *s = "forgreatjustice";
3 unsigned int add = 1;
4
5 s += add;
6 if (*s != 'o') return 1;
7
8 s += 1UL;
9 if (*s != 'r') return 2;
10
11 const char *s2 = (s += add);
12 if (*s2 != 'g') return 3;
13
14 s2 -= add;
15 if (*s2 != 'r') return 4;
16
17 return 0;
18}
19
20// run-translated-c
21// c_frontend=clang