Skip to content

Commit

Permalink
translate-c: fix translation of "ptr += uint"
Browse files Browse the repository at this point in the history
The right-hand side was incorrectly cast to a pointer, since only signed
ints were being interpreted correctly as pointer arithmetic.

Fixes ziglang#20285.
  • Loading branch information
djpohly authored and SammyJames committed Aug 7, 2024
1 parent 08df5e3 commit d9e0f17
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/translate_c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3824,8 +3824,9 @@ fn transCreateCompoundAssign(
const lhs_qt = getExprQualType(c, lhs);
const rhs_qt = getExprQualType(c, rhs);
const is_signed = cIsSignedInteger(lhs_qt);
const is_ptr_arithmetic = qualTypeIsPtr(lhs_qt) and cIsInteger(rhs_qt);
const is_ptr_op_signed = qualTypeIsPtr(lhs_qt) and cIsSignedInteger(rhs_qt);
const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_op_signed;
const requires_cast = !lhs_qt.eq(rhs_qt) and !is_ptr_arithmetic;

if (used == .unused) {
// common case
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
int main() {
const char *s = "forgreatjustice";
unsigned int add = 1;

s += add;
if (*s != 'o') return 1;

s += 1UL;
if (*s != 'r') return 2;

const char *s2 = (s += add);
if (*s2 != 'g') return 3;

s2 -= add;
if (*s2 != 'r') return 4;

return 0;
}

// run-translated-c
// c_frontend=clang

0 comments on commit d9e0f17

Please sign in to comment.