Skip to content

Commit

Permalink
Value: allocate enough limbs for bitwise xor result
Browse files Browse the repository at this point in the history
Closes #667
  • Loading branch information
ehaas authored and Vexu committed Apr 6, 2024
1 parent ce51123 commit 175d2a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/aro/Value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -739,9 +739,10 @@ pub fn bitXor(lhs: Value, rhs: Value, comp: *Compilation) !Value {
const lhs_bigint = lhs.toBigInt(&lhs_space, comp);
const rhs_bigint = rhs.toBigInt(&rhs_space, comp);

const extra = @intFromBool(lhs_bigint.positive != rhs_bigint.positive);
const limbs = try comp.gpa.alloc(
std.math.big.Limb,
@max(lhs_bigint.limbs.len, rhs_bigint.limbs.len),
@max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + extra,
);
defer comp.gpa.free(limbs);
var result_bigint = std.math.big.int.Mutable{ .limbs = limbs, .positive = undefined, .len = undefined };
Expand Down
7 changes: 7 additions & 0 deletions test/cases/binary expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ int invalid_ptr_arithmetic(struct Foo *num) {
return num - num;
}

_Static_assert((1 ^ -1) == -2, "");
_Static_assert((0 ^ 0) == 0, "");
_Static_assert((-1 ^ 0) == -1, "");
_Static_assert((-1 ^ -1) == 0, "");
_Static_assert((-2 ^ 2) == -4, "");


#define EXPECTED_ERRORS "binary expressions.c:3:7: error: invalid operands to binary expression ('long' and 'float')" \
"binary expressions.c:6:13: error: invalid operands to binary expression ('char' and 'int *')" \
"binary expressions.c:8:9: error: invalid operands to binary expression ('void (*)(void)' and 'void')" \
Expand Down

0 comments on commit 175d2a5

Please sign in to comment.