Skip to content

Sizeof should fail on incomplete types #5211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions regression/cbmc/incomplete-sizeof/array.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdlib.h>

int arr[];

int main(int argc, char **argv)
{
size_t s = sizeof(arr);
assert(s == 4);
}
9 changes: 9 additions & 0 deletions regression/cbmc/incomplete-sizeof/array.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
array.c

^EXIT=6$
^SIGNAL=0$
^file array.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
^CONVERSION ERROR$
--
^warning: ignoring
10 changes: 10 additions & 0 deletions regression/cbmc/incomplete-sizeof/enum.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdlib.h>

enum foo;

int main(int argc, char **argv)
{
size_t s = sizeof(enum foo);
assert(s == 0);
}
9 changes: 9 additions & 0 deletions regression/cbmc/incomplete-sizeof/enum.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
enum.c

^EXIT=6$
^SIGNAL=0$
^file enum.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
^CONVERSION ERROR$
--
^warning: ignoring
11 changes: 11 additions & 0 deletions regression/cbmc/incomplete-sizeof/struct.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <assert.h>
#include <stdlib.h>

struct foo;

int main(int argc, char **argv)
{
struct foo *thefoo = malloc(sizeof(struct foo));
size_t s = sizeof(struct foo);
assert(s == 0);
}
9 changes: 9 additions & 0 deletions regression/cbmc/incomplete-sizeof/struct.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
struct.c

^EXIT=6$
^SIGNAL=0$
^file struct.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
^CONVERSION ERROR$
--
^warning: ignoring
10 changes: 10 additions & 0 deletions regression/cbmc/incomplete-sizeof/union.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <assert.h>
#include <stdlib.h>

union foo;

int main(int argc, char **argv)
{
size_t s = sizeof(union foo);
assert(s == 0);
}
9 changes: 9 additions & 0 deletions regression/cbmc/incomplete-sizeof/union.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
union.c

^EXIT=6$
^SIGNAL=0$
^file union.c line \d+ function main: invalid application of \'sizeof\' to an incomplete type$
^CONVERSION ERROR$
--
^warning: ignoring
15 changes: 15 additions & 0 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,21 @@ void c_typecheck_baset::typecheck_expr_sizeof(exprt &expr)
}
else
{
if(
(type.id() == ID_struct_tag &&
follow_tag(to_struct_tag_type(type)).is_incomplete()) ||
(type.id() == ID_union_tag &&
follow_tag(to_union_tag_type(type)).is_incomplete()) ||
(type.id() == ID_c_enum_tag &&
follow_tag(to_c_enum_tag_type(type)).is_incomplete()) ||
(type.id() == ID_array && to_array_type(type).is_incomplete()))
{
error().source_location = expr.source_location();
error() << "invalid application of \'sizeof\' to an incomplete type\n\t\'"
<< to_string(type) << "\'" << eom;
throw 0;
}

auto size_of_opt = size_of_expr(type, *this);

if(!size_of_opt.has_value())
Expand Down