Skip to content

Visual Studio rejects pointer arithmetic and dereferencing of void* #6674

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
Feb 16, 2022
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
2 changes: 1 addition & 1 deletion regression/ansi-c/sizeof5/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c

^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc-cpp/Function_Arguments1/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.cpp

^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/null6/main.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
void *guard_malloc_counter = 0;
char *guard_malloc_counter = 0;

void *my_malloc(int size)
{
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/ptr_arithmetic_on_null/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c

^\[main.assertion.1\] line .* assertion \(void \*\)0 != \(void \*\)0 \+ \(.*\)1: SUCCESS$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer1/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c
--pointer-check
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer2/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c
--pointer-check --no-simplify --unwind 3
^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer3/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE broken-smt-backend gcc-only
main.c

^EXIT=0$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer5/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE broken-smt-backend
CORE broken-smt-backend gcc-only
main.c

^EXIT=10$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer6/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c
--pointer-check
^EXIT=10$
Expand Down
2 changes: 1 addition & 1 deletion regression/cbmc/void_pointer7/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CORE
CORE gcc-only
main.c
--pointer-check
^EXIT=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CORE
main.c
--enforce-contract foo
^.* error: void-typed expressions not allowed as assigns clause targets$
^.* error: (void-typed expressions not allowed as assigns clause targets|dereferencing void pointer)$
^CONVERSION ERROR$
^EXIT=(1|64)$
^SIGNAL=0$
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CORE
main.c
--enforce-contract foo
^.* error: void-typed expressions not allowed as assigns clause targets$
^.* error: (void-typed expressions not allowed as assigns clause targets|dereferencing void pointer)$
^CONVERSION ERROR$
^EXIT=(1|64)$
^SIGNAL=0$
Expand Down
5 changes: 2 additions & 3 deletions regression/contracts/assigns_enforce_side_effects_1/test.desc
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
CORE
main.c
--enforce-contract foo
^.*error: void-typed expressions not allowed as assigns clause targets$
^.*error: side-effects not allowed in assigns clause targets$
^.*error: ternary expressions not allowed in assigns clause targets$
activate-multi-line-match
.*error: (dereferencing void pointer|void-typed expressions not allowed as assigns clause targets\n.*\n.*error: side-effects not allowed in assigns clause targets\n.*\n.*error: ternary expressions not allowed in assigns clause targets\n)
^CONVERSION ERROR$
^EXIT=(1|64)$
^SIGNAL=0$
Expand Down
15 changes: 15 additions & 0 deletions regression/goto-cl/void-pointer/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
void *malloc(__CPROVER_size_t);
int printf(const char *, ...);

int main(int argc, char *argv[])
{
void *p = malloc(2);
printf("%p\n", p);
#ifdef VOID1
(void)*p;
#else
void *q = &p[1];
printf("%p\n", q);
#endif
return 0;
}
9 changes: 9 additions & 0 deletions regression/goto-cl/void-pointer/ptr-arithmetic.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c

error: pointer arithmetic with unknown object size
^CONVERSION ERROR$
^EXIT=(64|1)$
^SIGNAL=0$
--
Invariant check failed
9 changes: 9 additions & 0 deletions regression/goto-cl/void-pointer/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c
-DVOID1
error: dereferencing void pointer
^CONVERSION ERROR$
^EXIT=(64|1)$
^SIGNAL=0$
--
Invariant check failed
17 changes: 17 additions & 0 deletions src/ansi-c/c_typecheck_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,15 @@ void c_typecheck_baset::typecheck_expr_dereference(exprt &expr)
else if(op_type.id()==ID_pointer)
{
expr.type() = to_pointer_type(op_type).base_type();

if(
expr.type().id() == ID_empty &&
config.ansi_c.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
{
error().source_location = expr.source_location();
error() << "dereferencing void pointer" << eom;
throw 0;
}
}
else
{
Expand Down Expand Up @@ -3718,6 +3727,14 @@ void c_typecheck_baset::typecheck_arithmetic_pointer(const exprt &expr)
error() << "pointer arithmetic with unknown object size" << eom;
throw 0;
}
else if(
base_type.id() == ID_empty &&
config.ansi_c.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
{
error().source_location = expr.source_location();
error() << "pointer arithmetic with unknown object size" << eom;
throw 0;
}
}

void c_typecheck_baset::typecheck_expr_pointer_arithmetic(exprt &expr)
Expand Down