Преглед на файлове

Move casts to separate C file to prevent compiler from optimising them away

rl1987 преди 5 години
родител
ревизия
0bc9ed9d38
променени са 4 файла, в които са добавени 77 реда и са изтрити 6 реда
  1. 2 0
      src/test/include.am
  2. 50 0
      src/test/ptr_helpers.c
  3. 18 0
      src/test/ptr_helpers.h
  4. 7 6
      src/test/test_ptr_slow.c

+ 2 - 0
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 \

+ 50 - 0
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 <b> (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;
+}

+ 18 - 0
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 <stdint.h>
+
+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);

+ 7 - 6
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 <stdint.h>
 #include <limits.h>
@@ -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);