From ad437dd46db44f4693008585a3c46fd3e923dd51 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Sat, 6 Apr 2024 23:43:37 -0700 Subject: [PATCH] Parser: don't create pointer-to-invalid via ampersand operator Fixes #669 --- src/aro/Parser.zig | 16 +++++++++------- test/cases/binary expressions.c | 7 +++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index 7ac783c6..21245e09 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -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; diff --git a/test/cases/binary expressions.c b/test/cases/binary expressions.c index 3a6542ac..c628c61a 100644 --- a/test/cases/binary expressions.c +++ b/test/cases/binary expressions.c @@ -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, ""); @@ -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 *')" \ +