Skip to content

Commit 6684042

Browse files
committed
Extend sizeof check to incomplete enums and unions
And produce gcc-like error message.
1 parent 884c360 commit 6684042

File tree

8 files changed

+54
-13
lines changed

8 files changed

+54
-13
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
enum foo;
5+
6+
int main(int argc, char **argv)
7+
{
8+
size_t s = sizeof(enum foo);
9+
assert(s == 0);
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=6$
5+
^SIGNAL=0$
6+
^file main.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
7+
^CONVERSION ERROR$
8+
--
9+
^warning: ignoring
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=6$
5+
^SIGNAL=0$
6+
^file main.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
7+
^CONVERSION ERROR$
8+
--
9+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdlib.h>
3+
4+
union foo;
5+
6+
int main(int argc, char **argv)
7+
{
8+
size_t s = sizeof(union foo);
9+
assert(s == 0);
10+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=6$
5+
^SIGNAL=0$
6+
^file main.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
7+
^CONVERSION ERROR$
8+
--
9+
^warning: ignoring

regression/cbmc/incomplete-sizeof/test.desc

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,12 +952,15 @@ void c_typecheck_baset::typecheck_expr_sizeof(exprt &expr)
952952
else
953953
{
954954
if(
955-
type.id() == ID_struct_tag &&
956-
to_struct_type(follow_tag(to_struct_tag_type(type))).is_incomplete())
955+
(type.id() == ID_struct_tag &&
956+
to_struct_type(follow_tag(to_struct_tag_type(type))).is_incomplete()) ||
957+
(type.id() == ID_union_tag &&
958+
to_union_type(follow_tag(to_union_tag_type(type))).is_incomplete()) ||
959+
(type.id() == ID_c_enum && to_c_enum_type(type).is_incomplete()))
957960
{
958961
error().source_location = expr.source_location();
959-
error() << "calling sizeof for incomplete type: " << to_string(type)
960-
<< eom;
962+
error() << "invalid application of \'sizeof\' to an incomplete type\n\t\'"
963+
<< to_string(type) << "\'" << eom;
961964
throw 0;
962965
}
963966

0 commit comments

Comments
 (0)