From 0bc9ed9d38cb29783d000b6e22677ae16727976b Mon Sep 17 00:00:00 2001 From: rl1987 Date: Wed, 20 Mar 2019 18:54:11 +0200 Subject: [PATCH] Move casts to separate C file to prevent compiler from optimising them away --- src/test/include.am | 2 ++ src/test/ptr_helpers.c | 50 ++++++++++++++++++++++++++++++++++++++++ src/test/ptr_helpers.h | 18 +++++++++++++++ src/test/test_ptr_slow.c | 13 ++++++----- 4 files changed, 77 insertions(+), 6 deletions(-) create mode 100644 src/test/ptr_helpers.c create mode 100644 src/test/ptr_helpers.h diff --git a/src/test/include.am b/src/test/include.am index e6cebe1d1a..700107d6ce 100644 --- a/src/test/include.am +++ b/src/test/include.am @@ -211,6 +211,7 @@ src_test_test_slow_SOURCES += \ src/test/test_crypto_slow.c \ src/test/test_process_slow.c \ src/test/test_prob_distr.c \ + src/test/ptr_helpers.c \ src/test/test_ptr_slow.c \ src/test/testing_common.c \ src/test/testing_rsakeys.c \ @@ -315,6 +316,7 @@ noinst_HEADERS+= \ src/test/log_test_helpers.h \ src/test/rend_test_helpers.h \ src/test/test.h \ + src/test/ptr_helpers.h \ src/test/test_helpers.h \ src/test/test_dir_common.h \ src/test/test_connection.h \ diff --git a/src/test/ptr_helpers.c b/src/test/ptr_helpers.c new file mode 100644 index 0000000000..296238feeb --- /dev/null +++ b/src/test/ptr_helpers.c @@ -0,0 +1,50 @@ +/* Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2019, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include "ptr_helpers.h" + +/** + * Cast (inptr_t value) to a void pointer. + */ +void * +cast_intptr_to_voidstar(intptr_t x) +{ + void *r = (void *)x; + + return r; +} + +/** + * Cast x (void pointer) to inptr_t value. + */ +intptr_t +cast_voidstar_to_intptr(void *x) +{ + intptr_t r = (intptr_t)x; + + return r; +} + +/** + * Cast x (uinptr_t value) to void pointer. + */ +void * +cast_uintptr_to_voidstar(uintptr_t x) +{ + void *r = (void *)x; + + return r; +} + +/** + * Cast x (void pointer) to uinptr_t value. + */ +uintptr_t +cast_voidstar_to_uintptr(void *x) +{ + uintptr_t r = (uintptr_t)x; + + return r; +} diff --git a/src/test/ptr_helpers.h b/src/test/ptr_helpers.h new file mode 100644 index 0000000000..fe2c8c9705 --- /dev/null +++ b/src/test/ptr_helpers.h @@ -0,0 +1,18 @@ +/* Copyright (c) 2001-2004, Roger Dingledine. + * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson. + * Copyright (c) 2007-2019, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +#include + +void * +cast_intptr_to_voidstar(intptr_t x); + +intptr_t +cast_voidstar_to_intptr(void *x); + +void * +cast_uintptr_to_voidstar(uintptr_t x); + +uintptr_t +cast_voidstar_to_uintptr(void *x); diff --git a/src/test/test_ptr_slow.c b/src/test/test_ptr_slow.c index f064a3e7c2..07481fb1ec 100644 --- a/src/test/test_ptr_slow.c +++ b/src/test/test_ptr_slow.c @@ -6,6 +6,7 @@ #include "orconfig.h" #include "core/or/or.h" #include "test/test.h" +#include "test/ptr_helpers.h" #include #include @@ -15,9 +16,9 @@ static void assert_int_voidptr_roundtrip(int a) { intptr_t ap = (intptr_t)a; - void *b = (void *)ap; - intptr_t c = (intptr_t)b; - void *d = (void *)c; + void *b = cast_intptr_to_voidstar(ap); + intptr_t c = cast_voidstar_to_intptr(b); + void *d = cast_intptr_to_voidstar(c); tt_assert(ap == c); tt_assert(b == d); @@ -45,9 +46,9 @@ static void assert_uint_voidptr_roundtrip(unsigned int a) { uintptr_t ap = (uintptr_t)a; - void *b = (void *)ap; - uintptr_t c = (uintptr_t)b; - void *d = (void *)c; + void *b = cast_uintptr_to_voidstar(ap); + uintptr_t c = cast_voidstar_to_uintptr(b); + void *d = cast_uintptr_to_voidstar(c); tt_assert(ap == c); tt_assert(b == d);