Skip to content

Commit

Permalink
Parser: don't create pointer-to-invalid via ampersand operator
Browse files Browse the repository at this point in the history
Fixes #669
  • Loading branch information
ehaas authored and Vexu committed Apr 7, 2024
1 parent 175d2a5 commit 7757e64
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/aro/Parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6979,17 +6979,19 @@ fn unExpr(p: *Parser) Error!Result {
{
if (tree.isBitfield(member_node)) try p.errTok(.addr_of_bitfield, tok);
}
if (!tree.isLval(operand.node)) {
if (!tree.isLval(operand.node) and !operand.ty.is(.invalid)) {
try p.errTok(.addr_of_rvalue, tok);
}
if (operand.ty.qual.register) try p.errTok(.addr_of_register, tok);

const elem_ty = try p.arena.create(Type);
elem_ty.* = operand.ty;
operand.ty = Type{
.specifier = .pointer,
.data = .{ .sub_type = elem_ty },
};
if (!operand.ty.is(.invalid)) {
const elem_ty = try p.arena.create(Type);
elem_ty.* = operand.ty;
operand.ty = Type{
.specifier = .pointer,
.data = .{ .sub_type = elem_ty },
};
}
try operand.saveValue(p);
try operand.un(p, .addr_of_expr);
return operand;
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,11 @@ int invalid_ptr_arithmetic(struct Foo *num) {
return num - num;
}

void address_of_invalid_type(void) {
char x[64];
char *y = &(x&x);
}

_Static_assert((1 ^ -1) == -2, "");
_Static_assert((0 ^ 0) == 0, "");
_Static_assert((-1 ^ 0) == -1, "");
Expand Down Expand Up @@ -104,3 +109,5 @@ _Static_assert((-2 ^ 2) == -4, "");
"binary expressions.c:62:7: error: invalid operands to binary expression ('int' and 'float')" \
"binary expressions.c:67:5: error: used type 'void' where arithmetic or pointer type is required" \
"binary expressions.c:72:16: error: arithmetic on a pointer to an incomplete type 'struct Foo'" \
"binary expressions.c:77:18: error: invalid operands to binary expression ('char *' and 'char *')" \

0 comments on commit 7757e64

Please sign in to comment.