Skip to content

Commit a41a250

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

File tree

10 files changed

+75
-13
lines changed

10 files changed

+75
-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+
int arr[];
5+
6+
int main(int argc, char **argv)
7+
{
8+
size_t s = sizeof(arr);
9+
assert(s == 4);
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: 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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -952,12 +952,17 @@ 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_tag &&
960+
to_c_enum_type(follow_tag(to_c_enum_tag_type(type))).is_incomplete()) ||
961+
(type.id() == ID_array && to_array_type(type).is_incomplete()))
957962
{
958963
error().source_location = expr.source_location();
959-
error() << "calling sizeof for incomplete type: " << to_string(type)
960-
<< eom;
964+
error() << "invalid application of \'sizeof\' to an incomplete type\n\t\'"
965+
<< to_string(type) << "\'" << eom;
961966
throw 0;
962967
}
963968

0 commit comments

Comments
 (0)