| author | |
| committer | |
| log | ffb1a6d9a75a7c8804c0e9db0e23ac54265f447c |
| tree | b305e12bbf1ebbb64e1f9b3289e648c78404da8e |
| parent | 7829be6ee07aba6574fa5035c3d33023b3e23de7 |
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; |
| 3829 | 3830 | ||
| 3830 | if (used == .unused) { | 3831 | if (used == .unused) { |
| 3831 | // common case | 3832 | // common case |
test/cases/run_translated_c/compound_assignments_with_pointer_arithmetic.c created+21| ... | @@ -0,0 +1,21 @@ | ||
| 1 | int 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 | ||