]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Include scope ID in net::internet::address_v6::to_string()
authorJonathan Wakely <jwakely@redhat.com>
Fri, 12 Feb 2021 15:08:29 +0000 (15:08 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 12 Feb 2021 15:08:29 +0000 (15:08 +0000)
libstdc++-v3/ChangeLog:

* include/experimental/internet (address_v6::to_string): Include
scope ID in string.
* testsuite/experimental/net/internet/address/v6/members.cc:
Test to_string() results.

libstdc++-v3/include/experimental/internet
libstdc++-v3/testsuite/experimental/net/internet/address/v6/members.cc

index 57831676a29f7dcd9110657578f667e4affbf399..288c61ba25a9cb3c2adef3a91e50055b809249ae 100644 (file)
@@ -344,9 +344,23 @@ namespace ip
       to_string(const _Allocator& __a = _Allocator()) const
       {
        __string_with<_Allocator> __str(__a);
-       __str.resize(INET6_ADDRSTRLEN);
-       if (inet_ntop(AF_INET6, &_M_bytes, &__str.front(), __str.size()))
-         __str.erase(__str.find('\0'));
+       __str.resize(INET6_ADDRSTRLEN + (_M_scope_id ? 11 : 0));
+       char* const __p = &__str.front();
+       if (inet_ntop(AF_INET6, &_M_bytes, __p, __str.size()))
+         {
+           auto __end = __str.find('\0');
+           if (unsigned long __scope = _M_scope_id)
+             {
+               __end +=
+#if _GLIBCXX_USE_C99_STDIO
+                 __builtin_snprintf(__p + __end, __str.size() - __end,
+                                    "%%%lu", __scope);
+#else
+                 __builtin_sprintf(__p + __end, "%%%lu", __scope);
+#endif
+             }
+           __str.erase(__end);
+         }
        else
          __str.resize(0);
        return __str;
@@ -364,9 +378,7 @@ namespace ip
     static constexpr address_v6
     loopback() noexcept
     {
-      address_v6 __addr;
-      __addr._M_bytes[15] = 1;
-      return __addr;
+      return {bytes_type{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1}};
     }
 
   private:
index 506345dc99084fca975740533292b615cee0d52e..b77d6a29e3d6c9b70c5da2a0daa409fa1ecfad0d 100644 (file)
@@ -86,16 +86,30 @@ static_assert(test02(), "");
 void
 test03()
 {
+  // Assume these addresses use the preferred forms:
   VERIFY( address_v6::any().to_string() == "::" );
   VERIFY( address_v6::loopback().to_string() == "::1" );
+
+  // Choose values with no leading zeros, so output is portable:
+  address_v6 a{address_v6::bytes_type{21,22,23,24,25,26,27,28,29}, 42};
+  const std::string s = a.to_string();
+  if (s.find("::") != s.npos)
+    VERIFY( s == "1516:1718:191a:1b1c:1d00::%42" );
+  else
+  {
+    // Contiguous zeros were not shortened to "::"
+    VERIFY( s.substr(0, 25) == "1516:1718:191a:1b1c:1d00:" );
+    VERIFY( s.substr(s.size() - 3) == "%42" );
+  }
 }
 
 void
 test04()
 {
+  address_v6 a{address_v6::bytes_type{1,2,3,4,5,6,7,8,9}, 42};
   std::ostringstream ss;
-  ss << address_v6::any() << ' ' << address_v6::loopback();
-  VERIFY( ss.str() == ":: ::1" );
+  ss << address_v6::any() << ' ' << address_v6::loopback() << ' ' << a;
+  VERIFY( ss.str() == (":: ::1 " + a.to_string()) );
 }
 
 int