libstdc++: Fix errors in <experimental/internet>
authorJonathan Wakely <jwakely@redhat.com>
Fri, 12 Feb 2021 13:01:20 +0000 (13:01 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 12 Feb 2021 14:30:14 +0000 (14:30 +0000)
libstdc++-v3/ChangeLog:

* include/experimental/internet (address_v6::any): Avoid using
memcpy in constexpr function.
(address_v6::loopback): Likewise.
(make_address_v6): Fix missing return statements on error paths.
* include/experimental/io_context: Avoid -Wdangling-else
warning.
* testsuite/experimental/net/internet/address/v4/members.cc:
Remove unused variables.
* testsuite/experimental/net/internet/address/v6/members.cc:
New test.

libstdc++-v3/include/experimental/internet
libstdc++-v3/include/experimental/io_context
libstdc++-v3/testsuite/experimental/net/internet/address/v4/members.cc
libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc [new file with mode: 0644]

index 2fc253395f531a0724c44da6fcc7eb5e82d1ebdf..57831676a29f7dcd9110657578f667e4affbf399 100644 (file)
@@ -354,19 +354,18 @@ namespace ip
 #endif
 
     // static members:
+
     static constexpr address_v6
     any() noexcept
     {
-      address_v6 __addr;
-      __builtin_memcpy(&__addr._M_bytes, in6addr_any.s6_addr, 16);
-      return __addr;
+      return {};
     }
 
     static constexpr address_v6
     loopback() noexcept
     {
       address_v6 __addr;
-      __builtin_memcpy(&__addr._M_bytes, in6addr_loopback.s6_addr, 16);
+      __addr._M_bytes[15] = 1;
       return __addr;
     }
 
@@ -755,7 +754,10 @@ namespace ip
        __str++;
       }
     if (__out == std::end(__buf))
-      __ec = std::make_error_code(std::errc::invalid_argument);
+      {
+       __ec = std::make_error_code(std::errc::invalid_argument);
+       return {};
+      }
     else
       {
        *__out = '\0';
@@ -790,7 +792,10 @@ namespace ip
        __n++;
       }
     if (__out == std::end(__buf))
-      __ec = std::make_error_code(std::errc::invalid_argument);
+      {
+       __ec = std::make_error_code(std::errc::invalid_argument);
+       return {};
+      }
     else
       {
        *__out = '\0';
@@ -835,7 +840,10 @@ namespace ip
        __n++;
       }
     if (__out == std::end(__buf))
-      __ec = std::make_error_code(std::errc::invalid_argument);
+      {
+       __ec = std::make_error_code(std::errc::invalid_argument);
+       return {};
+      }
     else
       {
        *__out = '\0';
index 12c269b4af28a6a98febd0de0323050a21004794..c82f30cd119cc06f025b5a5c94da2b7a9dbb4d9c 100644 (file)
@@ -680,10 +680,12 @@ inline namespace v1
            continue;
 
          if (__res == __reactor::_S_timeout)
-           if (__timerq == nullptr)
-             return false;
-           else
-             continue;  // timed out, so restart loop and process the timer
+           {
+             if (__timerq == nullptr)
+               return false;
+             else
+               continue;  // timed out, so restart loop and process the timer
+           }
 
          __timerq = nullptr;
 
index f0eff14f202c5c79ae8b1a1432f4049b46c3a6e5..f644c0847ab3128c6a3f2b946421b5f44937b54c 100644 (file)
 // { dg-add-options net_ts }
 
 #include <experimental/internet>
+#include <sstream>
 #include <testsuite_hooks.h>
 
 using std::experimental::net::ip::address_v4;
 
-void
+constexpr bool
 test01()
 {
-  bool test __attribute__((unused)) = false;
-
   address_v4 a;
   VERIFY( a.is_unspecified() );
 
@@ -39,13 +38,15 @@ test01()
 
   a = address_v4::broadcast();
   VERIFY( !a.is_unspecified() );
+
+  return true;
 }
 
-void
+static_assert(test01(), "");
+
+constexpr bool
 test02()
 {
-  bool test __attribute__((unused)) = false;
-
   auto a = address_v4::loopback();
   VERIFY( a.is_loopback() );
 
@@ -63,13 +64,15 @@ test02()
 
   a = address_v4::broadcast();
   VERIFY( !a.is_loopback() );
+
+  return true;
 }
 
-void
+static_assert(test02(), "");
+
+constexpr bool
 test03()
 {
-  bool test __attribute__((unused)) = false;
-
   auto a = address_v4{0xE0000001};
   VERIFY( a.is_multicast() );
 
@@ -84,13 +87,15 @@ test03()
 
   a = address_v4{0xDFFFFFFF};
   VERIFY( !a.is_multicast() );
+
+  return true;
 }
 
+static_assert(test03(), "");
+
 void
 test04()
 {
-  bool test __attribute__((unused)) = false;
-
   VERIFY( address_v4::any().to_string() == "0.0.0.0" );
   VERIFY( address_v4::loopback().to_string() == "127.0.0.1" );
   VERIFY( address_v4::broadcast().to_string() == "255.255.255.255" );
@@ -99,15 +104,12 @@ test04()
 void
 test05()
 {
-  bool test __attribute__((unused)) = false;
-
   std::ostringstream ss;
   ss << address_v4::any() << ' ' << address_v4::loopback() << ' '
     << address_v4::broadcast();
   VERIFY( ss.str() == "0.0.0.0 127.0.0.1 255.255.255.255" );
 }
 
-
 int
 main()
 {
diff --git a/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc b/libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc
new file mode 100644 (file)
index 0000000..506345d
--- /dev/null
@@ -0,0 +1,108 @@
+// Copyright (C) 2021 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-do run { target c++14 } }
+// { dg-add-options net_ts }
+
+#include <experimental/internet>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+using std::experimental::net::ip::address_v6;
+
+constexpr bool
+test01()
+{
+  address_v6 a;
+  VERIFY( a.is_unspecified() );
+  VERIFY( !a.is_loopback() );
+  VERIFY( !a.is_multicast() );
+  VERIFY( !a.is_link_local() );
+  VERIFY( !a.is_site_local() );
+
+  a = address_v6::any();
+  VERIFY( a.is_unspecified() );
+  VERIFY( !a.is_loopback() );
+  VERIFY( !a.is_multicast() );
+  VERIFY( !a.is_link_local() );
+  VERIFY( !a.is_site_local() );
+
+  a = address_v6::loopback();
+  VERIFY( !a.is_unspecified() );
+  VERIFY( a.is_loopback() );
+  VERIFY( !a.is_multicast() );
+  VERIFY( !a.is_link_local() );
+  VERIFY( !a.is_site_local() );
+
+  a = address_v6{address_v6::loopback().to_bytes(), 1};
+  VERIFY( !a.is_unspecified() );
+  VERIFY( !a.is_loopback() );
+  VERIFY( !a.is_multicast() );
+  VERIFY( !a.is_link_local() );
+  VERIFY( !a.is_site_local() );
+
+  return true;
+}
+
+static_assert(test01(), "");
+
+constexpr bool
+test02()
+{
+  auto a = address_v6{address_v6::bytes_type{0xFF}};
+  VERIFY( a.is_multicast() );
+
+  a = address_v6{address_v6::bytes_type{0xFF, 0x01}};
+  VERIFY( a.is_multicast() );
+
+  a = address_v6{address_v6::bytes_type{0xFF, 0x00, 0x01}};
+  VERIFY( a.is_multicast() );
+
+  a = address_v6{address_v6::bytes_type{0xFE, 0x80}};
+  VERIFY( !a.is_multicast() );
+
+  a = address_v6{address_v6::bytes_type{0xFE, 0xC0}};
+  VERIFY( !a.is_multicast() );
+
+  return true;
+}
+
+static_assert(test02(), "");
+
+void
+test03()
+{
+  VERIFY( address_v6::any().to_string() == "::" );
+  VERIFY( address_v6::loopback().to_string() == "::1" );
+}
+
+void
+test04()
+{
+  std::ostringstream ss;
+  ss << address_v6::any() << ' ' << address_v6::loopback();
+  VERIFY( ss.str() == ":: ::1" );
+}
+
+int
+main()
+{
+  test01();
+  test02();
+  test03();
+  test04();
+}