From 57cec3011d65472d010d8ce92c9e4fc7b5e80a0b Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Mon, 1 Apr 2024 11:35:45 -0700 Subject: [PATCH] Parser: do not attempt to cast floats to incomplete-size ints Fixes #660 --- src/aro/Parser.zig | 4 ++++ test/cases/casts.c | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/aro/Parser.zig b/src/aro/Parser.zig index 13c6bed9..5507e0ae 100644 --- a/src/aro/Parser.zig +++ b/src/aro/Parser.zig @@ -5962,6 +5962,10 @@ pub const Result = struct { if (to.is(.bool)) { res.val.boolCast(p.comp); } else if (old_float and new_int) { + if (to.hasIncompleteSize()) { + try p.errStr(.cast_to_incomplete_type, l_paren, try p.typeStr(to)); + return error.ParsingFailed; + } // Explicit cast, no conversion warning _ = try res.val.floatToInt(to, p.comp); } else if (new_float and old_int) { diff --git a/test/cases/casts.c b/test/cases/casts.c index 86607458..f0a48ce0 100644 --- a/test/cases/casts.c +++ b/test/cases/casts.c @@ -16,6 +16,7 @@ void foo(void) { (enum E)1; int *ptr; ptr = (int *)(void)5; + (enum DoesNotExist)2.0; } #define EXPECTED_ERRORS "casts.c:5:5: error: cannot cast to non arithmetic or pointer type 'struct Foo'" \ @@ -27,4 +28,5 @@ void foo(void) { "casts.c:11:13: error: pointer cannot be cast to type 'float'" \ "casts.c:16:5: error: cast to incomplete type 'enum E'" \ "casts.c:18:18: error: used type 'void' where arithmetic or pointer type is required" \ + "casts.c:19:5: error: cast to incomplete type 'enum DoesNotExist'" \