Fix ipv6 address parsing with leading compressor (dotnet/corefx#38654)
authorMarco Rossignoli <marco.rossignoli@gmail.com>
Thu, 20 Jun 2019 14:24:33 +0000 (16:24 +0200)
committerDavid Shulman <david.shulman@microsoft.com>
Thu, 20 Jun 2019 14:24:33 +0000 (07:24 -0700)
* fix ipv6 parse

* address PR feedback

* address PR feedback

Commit migrated from https://github.com/dotnet/corefx/commit/600469a826ecd26ea5710847557819eff78e3759

src/libraries/Common/src/System/Net/IPv6AddressHelper.Common.cs
src/libraries/System.Net.Primitives/tests/FunctionalTests/IPAddressParsing.cs
src/libraries/System.Private.Uri/tests/FunctionalTests/UriIpHostTest.cs

index b0c85e3..10a8c56 100644 (file)
@@ -411,10 +411,15 @@ namespace System
                 int toIndex = NumberOfLabels - 1;
                 int fromIndex = index - 1;
 
-                for (int i = index - compressorIndex; i > 0; --i)
+                // if fromIndex and toIndex are the same, it means that "zero bits" are already in the correct place
+                // it happens for leading and trailing compression
+                if (fromIndex != toIndex)
                 {
-                    numbers[toIndex--] = numbers[fromIndex];
-                    numbers[fromIndex--] = 0;
+                    for (int i = index - compressorIndex; i > 0; --i)
+                    {
+                        numbers[toIndex--] = numbers[fromIndex];
+                        numbers[fromIndex--] = 0;
+                    }
                 }
             }
         }
index 8cc47e5..fe8720e 100644 (file)
@@ -255,6 +255,8 @@ namespace System.Net.Primitives.Functional.Tests
             new object[] { "E:E:E:E:E:0:0:1", "E:E:E:E:E::1" },
             new object[] { "E:E:E:E:E:0:2:2", "E:E:E:E:E:0:2:2" },
             new object[] { "E:E:E:E:E:E:0:1", "E:E:E:E:E:E:0:1" },
+            new object[] { "::2:3:4:5:6:7:8", "0:2:3:4:5:6:7:8" },
+            new object[] { "1:2:3:4:5:6:7::", "1:2:3:4:5:6:7:0" },
             new object[] { "::FFFF:192.168.0.1", "::FFFF:192.168.0.1" },
             new object[] { "::FFFF:0.168.0.1", "::FFFF:0.168.0.1" },
             new object[] { "::0.0.255.255", "::FFFF" },
index 35d2258..fa53221 100644 (file)
@@ -282,6 +282,8 @@ namespace System.PrivateUri.Tests
         [InlineData("1:0:0:1:0:0:1:1", "1::1:0:0:1:1")]
         [InlineData("1:1:0:0:0:1:0:1", "1:1::1:0:1")]
         [InlineData("1:0:0:0:1:0:1:1", "1::1:0:1:1")]
+        [InlineData("::2:3:4:5:6:7:8", "0:2:3:4:5:6:7:8")]
+        [InlineData("1:2:3:4:5:6:7::", "1:2:3:4:5:6:7:0")]
         public void UriIPv6Host_CompressionRangeSelection_Success(string address, string expected)
         {
             ParseIPv6Address(address, expected);