From 84d4fff5dd1bb40b12247c6e2bf033acdc5dbd4a Mon Sep 17 00:00:00 2001 From: Clinton Ingram Date: Tue, 24 Feb 2026 16:59:59 -0800 Subject: [PATCH 1/2] improve Adler32 NMax test --- .../System.IO.Hashing/tests/Adler32Tests.cs | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs b/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs index 5b39a00669abf5..6f48eace92fb8c 100644 --- a/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs +++ b/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs @@ -152,19 +152,31 @@ public void VerifyHashToUInt32(TestCase testCase) } [Theory] - [InlineData(5553, 0xAA40476Bu)] - [InlineData(11104, 0xA2778E87u)] + [InlineData(5553, 0x62C69C89u)] + [InlineData(11104, 0xA8AE3724u)] public void LargeInput_ExceedsNMax(int length, uint expected) { - byte[] data = new byte[length]; - for (int i = 0; i < data.Length; i++) - { - data[i] = (byte)('a' + (i % 26)); - } - - Assert.Equal(expected, Adler32.HashToUInt32(data)); + // This test ensures that Adler32 optimizations involving delayed modulo + // do not overflow a 32-bit intermediate at any point. var alg = new Adler32(); + + // The maximum possible value of an Adler32 checksum is 0xFFF0FFF0, + // which has both components just below the modulo value (0xFFF0 == 65520). + // A sequence of 655519 ones will generate this value. + + byte[] primer = new byte[65519]; + primer.AsSpan().Fill(1); + + alg.Append(primer); + Assert.Equal(0xFFF0FFF0, alg.GetCurrentHashAsUInt32()); + + // Starting from an alerady-maxed checksum, a stream of 5553 max value + // bytes will overflow if not reduced by mod 65521 before the last byte. + + byte[] data = new byte[length]; + data.AsSpan().Fill(byte.MaxValue); + alg.Append(data); Assert.Equal(expected, alg.GetCurrentHashAsUInt32()); } From cb09e6ee54a6e5403d2b69a4b07421ed281dd1a6 Mon Sep 17 00:00:00 2001 From: Clinton Ingram Date: Tue, 24 Feb 2026 17:10:40 -0800 Subject: [PATCH 2/2] comment typos --- src/libraries/System.IO.Hashing/tests/Adler32Tests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs b/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs index 6f48eace92fb8c..a478120b5ffb8d 100644 --- a/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs +++ b/src/libraries/System.IO.Hashing/tests/Adler32Tests.cs @@ -163,7 +163,7 @@ public void LargeInput_ExceedsNMax(int length, uint expected) // The maximum possible value of an Adler32 checksum is 0xFFF0FFF0, // which has both components just below the modulo value (0xFFF0 == 65520). - // A sequence of 655519 ones will generate this value. + // A sequence of 65519 ones will generate this value. byte[] primer = new byte[65519]; primer.AsSpan().Fill(1); @@ -171,7 +171,7 @@ public void LargeInput_ExceedsNMax(int length, uint expected) alg.Append(primer); Assert.Equal(0xFFF0FFF0, alg.GetCurrentHashAsUInt32()); - // Starting from an alerady-maxed checksum, a stream of 5553 max value + // Starting from an already-maxed checksum, a stream of 5553 max value // bytes will overflow if not reduced by mod 65521 before the last byte. byte[] data = new byte[length];