d889bc80be5a2389240a24b30873138a54991e10 — Michael Forney 21 days ago 8589d9d master
expr: Disallow function/incomplete types and bit-fields in sizeof/_Alignof
1 files changed, 11 insertions(+), 5 deletions(-)

M expr.c
M expr.c => expr.c +11 -5
@@ 842,18 842,24 @@ unaryexpr(struct scope *s)
 				expect(TRPAREN, "after expression");
 				if (op == TSIZEOF)
 					e = postfixexpr(s, e);
-				if (e->decayed)
-					e = e->base;
-				t = e->type;
 			}
 		} else if (op == TSIZEOF) {
+			t = NULL;
 			e = unaryexpr(s);
+		} else {
+			error(&tok.loc, "expected ')' after '_Alignof'");
+		}
+		if (!t) {
 			if (e->decayed)
 				e = e->base;
+			if (e->kind == EXPRBITFIELD)
+				error(&tok.loc, "%s operator applied to bitfield expression", tokstr[op]);
 			t = e->type;
-		} else {
-			error(&tok.loc, "expected ')' after '_Alignof'");
 		}
+		if (t->incomplete)
+			error(&tok.loc, "%s operator applied to incomplete type", tokstr[op]);
+		if (t->kind == TYPEFUNC)
+			error(&tok.loc, "%s operator applied to function type", tokstr[op]);
 		e = mkconstexpr(&typeulong, op == TSIZEOF ? t->size : t->align);
 		break;
 	default: