@@ -11,6 +11,7 @@ import (
11
11
"crypto/rand"
12
12
"encoding"
13
13
"encoding/hex"
14
+ "fmt"
14
15
"hash"
15
16
"io"
16
17
"testing"
@@ -831,6 +832,62 @@ func TestBlockGeneric(t *testing.T) {
831
832
}
832
833
}
833
834
835
+ // Tests for unmarshaling hashes that have hashed a large amount of data
836
+ // The initial hash generation is omitted from the test, because it takes a long time.
837
+ // The test contains some already-generated states, and their expected sums
838
+ // Tests a problem that is outlined in Github issue #29541
839
+ // The problem is triggered when an amount of data has been hashed for which
840
+ // the data length has a 1 in the 32nd bit. When casted to int, this changes
841
+ // the sign of the value, and causes the modulus operation to return a
842
+ // different result.
843
+ type unmarshalTest struct {
844
+ state string
845
+ sum string
846
+ }
847
+
848
+ var largeUnmarshalTests = []unmarshalTest {
849
+ // Data length: 6_565_544_823
850
+ unmarshalTest {
851
+ state : "sha\a ηe\x0f \x0f \xe1 r]#\a oJ!.{5B\xe4 \x14 0\x91 \xdd \x00 a\xe1 \xb3 E&\xb9 \xbb \a J\x9f ^\x9f \x03 ͺD\x96 H\x80 \xb0 X\x9d \xde ʸ\f \xf7 :\xd5 \xe6 '\xb9 \x93 f\xdd A\xf0 ~\xe1 \x02 \x14 \x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \b \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \x1b \x1c \x1d \x1e \x1f !\" #$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\ ]^_`abcdefghijklmnopqrstuv\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x01 \x87 VCw" ,
852
+ sum : "12d612357a1dbc74a28883dff79b83e7d2b881ae40d7a67fd7305490bc8a641cd1ce9ece598192080d6e9ac7e75d5988567a58a9812991299eb99a04ecb69523" ,
853
+ },
854
+ unmarshalTest {
855
+ state : "sha\a 2\xd2 \xdc \xf5 \xd7 \xe2 \xf9 \x97 \xaa \xe7 }Fϱ\xbc \x8e \xbf \x12 h\x83 Z\xa1 \xc7 \xf5 p>bfS T\xea \xee \x1e \xa6 Z\x9c \xa4 ڶ\u0086 \b n\xe4 7\x8f sGs3\xe0 \xda \\ \x9d qZ\xa5 \xf6 \xd0 kM\xa1 \xf2 \x00 \x01 \x02 \x03 \x04 \x05 \x06 \a \b \t \n \v \f \r \x0e \x0f \x10 \x11 \x12 \x13 \x14 \x15 \x16 \x17 \x18 \x19 \x1a \x1b \x1c \x1d \x1e \x1f !\" #$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\ ]^_`abcdefghijklmnopqrstuv\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x01 \xa7 VCw" ,
856
+ sum : "94a04b9a901254cd94ca0313557e4be3ab1ca86e920c1f3efdc22d361e9ae12be66bc6d6dc5db79a0a4aa6eca6f293c1e9095bbae127ae405f6c325478343299" ,
857
+ },
858
+ }
859
+
860
+ func safeSum (h hash.Hash ) (sum []byte , err error ) {
861
+ defer func () {
862
+ if r := recover (); r != nil {
863
+ err = fmt .Errorf ("sum panic: %v" , r )
864
+ }
865
+ }()
866
+
867
+ return h .Sum (nil ), nil
868
+ }
869
+
870
+ func TestLargeHashes (t * testing.T ) {
871
+ for i , test := range largeUnmarshalTests {
872
+
873
+ h := New ()
874
+ if err := h .(encoding.BinaryUnmarshaler ).UnmarshalBinary ([]byte (test .state )); err != nil {
875
+ t .Errorf ("test %d could not unmarshal: %v" , i , err )
876
+ continue
877
+ }
878
+
879
+ sum , err := safeSum (h )
880
+ if err != nil {
881
+ t .Errorf ("test %d could not sum: %v" , i , err )
882
+ continue
883
+ }
884
+
885
+ if fmt .Sprintf ("%x" , sum ) != test .sum {
886
+ t .Errorf ("test %d sum mismatch: expect %s got %x" , i , test .sum , sum )
887
+ }
888
+ }
889
+ }
890
+
834
891
var bench = New ()
835
892
var buf = make ([]byte , 8192 )
836
893
0 commit comments