Skip to content

Commit e8e9288

Browse files
committed
Visual Studio rejects pointer arithmetic and dereferencing of void*
Pointer arithmetic using void* is a (document) GCC extension. Dereferencing void* pointers is permitted by GCC when the value is unused. Visual Studio rejects both. This required updates to tests to either not rely on pointer arithmetic over void*, or restrict tests to GCC only. Fixes: #5275
1 parent 12ef9e7 commit e8e9288

File tree

15 files changed

+77
-9
lines changed

15 files changed

+77
-9
lines changed

regression/ansi-c/sizeof5/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33

44
^EXIT=0$

regression/cbmc/null6/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
void *guard_malloc_counter = 0;
1+
char *guard_malloc_counter = 0;
22

33
void *my_malloc(int size)
44
{

regression/cbmc/ptr_arithmetic_on_null/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33

44
^\[main.assertion.1\] line .* assertion \(void \*\)0 != \(void \*\)0 \+ \(.*\)1: SUCCESS$

regression/cbmc/void_pointer1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33
--pointer-check
44
^EXIT=0$

regression/cbmc/void_pointer2/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33
--pointer-check --no-simplify --unwind 3
44
^EXIT=0$

regression/cbmc/void_pointer3/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE broken-smt-backend
1+
CORE broken-smt-backend gcc-only
22
main.c
33

44
^EXIT=0$

regression/cbmc/void_pointer5/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE broken-smt-backend
1+
CORE broken-smt-backend gcc-only
22
main.c
33

44
^EXIT=10$

regression/cbmc/void_pointer6/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33
--pointer-check
44
^EXIT=10$

regression/cbmc/void_pointer7/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
CORE
1+
CORE gcc-only
22
main.c
33
--pointer-check
44
^EXIT=0$
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
void *malloc(__CPROVER_size_t);
2+
int printf(const char *, ...);
3+
4+
int main(int argc, char *argv[])
5+
{
6+
void *p = malloc(2);
7+
printf("%p\n", p);
8+
#ifdef VOID1
9+
(void)*p;
10+
#else
11+
void *q = &p[1];
12+
printf("%p\n", q);
13+
#endif
14+
return 0;
15+
}
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+
error: pointer arithmetic with unknown object size
5+
^CONVERSION ERROR$
6+
^EXIT=(64|1)$
7+
^SIGNAL=0$
8+
--
9+
Invariant check failed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE test-c++-front-end
2+
main.c
3+
-Dvoid_global
4+
^EXIT=(64|1)$
5+
^SIGNAL=0$
6+
void-typed symbol not permitted
7+
^CONVERSION ERROR$
8+
--
9+
Invariant check failed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE test-c++-front-end
2+
main.c
3+
4+
^EXIT=(64|1)$
5+
^SIGNAL=0$
6+
void-typed symbol not permitted
7+
^CONVERSION ERROR$
8+
--
9+
Invariant check failed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
-DVOID1
4+
error: dereferencing void pointer
5+
^CONVERSION ERROR$
6+
^EXIT=(64|1)$
7+
^SIGNAL=0$
8+
--
9+
Invariant check failed

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,6 +1784,15 @@ void c_typecheck_baset::typecheck_expr_dereference(exprt &expr)
17841784
else if(op_type.id()==ID_pointer)
17851785
{
17861786
expr.type() = to_pointer_type(op_type).base_type();
1787+
1788+
if(
1789+
expr.type().id() == ID_empty &&
1790+
config.ansi_c.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
1791+
{
1792+
error().source_location = expr.source_location();
1793+
error() << "dereferencing void pointer" << eom;
1794+
throw 0;
1795+
}
17871796
}
17881797
else
17891798
{
@@ -3718,6 +3727,14 @@ void c_typecheck_baset::typecheck_arithmetic_pointer(const exprt &expr)
37183727
error() << "pointer arithmetic with unknown object size" << eom;
37193728
throw 0;
37203729
}
3730+
else if(
3731+
base_type.id() == ID_empty &&
3732+
config.ansi_c.mode == configt::ansi_ct::flavourt::VISUAL_STUDIO)
3733+
{
3734+
error().source_location = expr.source_location();
3735+
error() << "pointer arithmetic with unknown object size" << eom;
3736+
throw 0;
3737+
}
37213738
}
37223739

37233740
void c_typecheck_baset::typecheck_expr_pointer_arithmetic(exprt &expr)

0 commit comments

Comments
 (0)