Browse Source

Use void * as memory buffers, not char *

For one, many builtin functions do this (memcpy, read, etc). And, it
would be more correct to use unsigned char * (because of aliasing
rules), but that makes C++ unhappy when passing string literals there.

Got rid of -fno-strict-aliasing in sockcreator tests.

git-svn-id: svn://bind10.isc.org/svn/bind10/branches/vorner-sockcreator@3186 e5f2f494-b856-4b98-b285-d166d9295462
Michal Vaner 14 years ago
parent
commit
e88330e1db

+ 0 - 5
src/bin/sockcreator/tests/Makefile.am

@@ -1,11 +1,6 @@
 CLEANFILES = *.gcno *.gcda
 
 AM_CPPFLAGS = -I$(top_srcdir)/src/lib -I$(top_builddir)/src/lib
-# This is here because of juggling with sockaddr pointer.
-# It is somehow required by the BSD socket interface, but it
-# breaks C++ strict aliasing rules, so we need to ask the compiler
-# not to use them.
-AM_CPPFLAGS += -fno-strict-aliasing
 AM_CXXFLAGS = $(B10_CXXFLAGS)
 
 if USE_STATIC_LINK

+ 11 - 9
src/bin/sockcreator/tests/sockcreator_tests.cc

@@ -39,8 +39,8 @@ namespace {
  * This is a macro so ASSERT_* does abort the TEST, not just the
  * function inside.
  */
-#define TEST_ANY_CREATE(SOCK_TYPE, ADDR_TYPE, ADDR_FAMILY, ADDR_SET, \
-    CHECK_SOCK) \
+#define TEST_ANY_CREATE(SOCK_TYPE, ADDR_TYPE, ADDR_FAMILY, FAMILY_FIELD, \
+    ADDR_SET, CHECK_SOCK) \
     do { \
         /*
          * This should create an address that binds on all interfaces
@@ -49,9 +49,9 @@ namespace {
         struct ADDR_TYPE addr; \
         memset(&addr, 0, sizeof addr); \
         ADDR_SET(addr); \
+        addr.FAMILY_FIELD = ADDR_FAMILY; \
         struct sockaddr *addr_ptr = static_cast<struct sockaddr *>( \
             static_cast<void *>(&addr)); \
-        addr_ptr->sa_family = ADDR_FAMILY; \
         \
         int socket = get_sock(SOCK_TYPE, addr_ptr, sizeof addr); \
         /* Provide even nice error message. */ \
@@ -89,21 +89,23 @@ namespace {
  * Several tests to ensure we can create the sockets.
  */
 TEST(get_sock, udp4_create) {
-    TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in, AF_INET, INADDR_SET, UDP_CHECK);
+    TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in, AF_INET, sin_family, INADDR_SET,
+        UDP_CHECK);
 }
 
 TEST(get_sock, tcp4_create) {
-    TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in, AF_INET, INADDR_SET, TCP_CHECK);
+    TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in, AF_INET, sin_family, INADDR_SET,
+        TCP_CHECK);
 }
 
 TEST(get_sock, udp6_create) {
-    TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in6, AF_INET6, IN6ADDR_SET,
-        UDP_CHECK);
+    TEST_ANY_CREATE(SOCK_DGRAM, sockaddr_in6, AF_INET6, sin6_family,
+        IN6ADDR_SET, UDP_CHECK);
 }
 
 TEST(get_sock, tcp6_create) {
-    TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in6, AF_INET6, IN6ADDR_SET,
-        TCP_CHECK);
+    TEST_ANY_CREATE(SOCK_STREAM, sockaddr_in6, AF_INET6, sin6_family,
+        IN6ADDR_SET, TCP_CHECK);
 }
 
 /*

+ 4 - 2
src/lib/util/io/fd.cc

@@ -22,7 +22,8 @@ namespace util {
 namespace io {
 
 bool
-write_data(const int fd, const char *buffer, const size_t length) {
+write_data(const int fd, const void *buffer_v, const size_t length) {
+    const unsigned char *buffer(static_cast<const unsigned char *>(buffer_v));
     size_t rest(length);
     // Just keep writing until all is written
     while (rest) {
@@ -42,7 +43,8 @@ write_data(const int fd, const char *buffer, const size_t length) {
 }
 
 ssize_t
-read_data(const int fd, char *buffer, const size_t length) {
+read_data(const int fd, void *buffer_v, const size_t length) {
+    unsigned char *buffer(static_cast<unsigned char *>(buffer_v));
     size_t rest(length), already(0);
     while (rest) { // Stil something to read
         ssize_t amount(read(fd, buffer, rest));

+ 2 - 2
src/lib/util/io/fd.h

@@ -39,7 +39,7 @@ namespace io {
  * \param length How much data is there to write.
  */
 bool
-write_data(const int fd, const char *data, const size_t length);
+write_data(const int fd, const void *data, const size_t length);
 
 /*
  * \short read() that reads everything.
@@ -52,7 +52,7 @@ write_data(const int fd, const char *data, const size_t length);
  * \param length How many of them.
  */
 ssize_t
-read_data(const int fd, char *buffer, const size_t length);
+read_data(const int fd, void *buffer, const size_t length);
 
 }
 }

+ 8 - 4
src/lib/util/unittests/fork.cc

@@ -69,7 +69,8 @@ process_ok(pid_t process) {
  * Used to provide the input in non-blocking/asynchronous way.
  */
 pid_t
-provide_input(int *read_pipe, const char *input, const size_t length) {
+provide_input(int *read_pipe, const void *input, const size_t length)
+{
     int pipes[2];
     if (pipe(pipes)) {
         return -1;
@@ -94,7 +95,8 @@ provide_input(int *read_pipe, const char *input, const size_t length) {
  * with given data. Used to check output of run in asynchronous way.
  */
 pid_t
-check_output(int *write_pipe, const char *output, const size_t length) {
+check_output(int *write_pipe, const void *output, const size_t length)
+{
     int pipes[2];
     if (pipe(pipes)) {
         return -1;
@@ -105,7 +107,7 @@ check_output(int *write_pipe, const char *output, const size_t length) {
         return pid;
     } else {
         close(pipes[1]);
-        char buffer[length + 1];
+        unsigned char buffer[length + 1];
         // Try to read one byte more to see if the output ends here
         size_t got_length(read_data(pipes[0], buffer, length + 1));
         bool ok(true);
@@ -116,13 +118,15 @@ check_output(int *write_pipe, const char *output, const size_t length) {
             ok = false;
         }
         if(!ok || memcmp(buffer, output, length)) {
+            const unsigned char *output_c(static_cast<const unsigned char *>(
+                output));
             // If the differ, print what we have
             for(size_t i(0); i != got_length; ++ i) {
                 fprintf(stderr, "%02hhx", buffer[i]);
             }
             fprintf(stderr, "\n");
             for(size_t i(0); i != length; ++ i) {
-                fprintf(stderr, "%02hhx", output[i]);
+                fprintf(stderr, "%02hhx", output_c[i]);
             }
             fprintf(stderr, "\n");
             exit(1);

+ 2 - 2
src/lib/util/unittests/fork.h

@@ -40,10 +40,10 @@ bool
 process_ok(pid_t process);
 
 pid_t
-provide_input(int *read_pipe, const char *input, const size_t length);
+provide_input(int *read_pipe, const void *input, const size_t length);
 
 pid_t
-check_output(int *write_pipe, const char *output, const size_t length);
+check_output(int *write_pipe, const void *output, const size_t length);
 
 } // End of the namespace
 }