]> Cypherpunks repositories - gostls13.git/commitdiff
asn1 incorrectly encoded signed integers. When determining the
authorNicholas Waples <nwaples@gmail.com>
Mon, 9 Aug 2010 14:25:54 +0000 (10:25 -0400)
committerAdam Langley <agl@golang.org>
Mon, 9 Aug 2010 14:25:54 +0000 (10:25 -0400)
encoded length it was not taking into account the sign bit.

Fixes #997.

R=agl1, gri
CC=golang-dev
https://golang.org/cl/1870047

src/pkg/asn1/marshal.go
src/pkg/asn1/marshal_test.go

index d4f8f782d450a9f37eec95d525920cba4d7fef94..328042b2b2c159650e1bb72d66738142bb8f63ef 100644 (file)
@@ -123,13 +123,20 @@ func marshalInt64(out *forkableWriter, i int64) (err os.Error) {
 }
 
 func int64Length(i int64) (numBytes int) {
-       if i == 0 {
-               return 1
+       numBytes = 1
+
+       if i > 0 {
+               for i > 127 {
+                       numBytes++
+                       i >>= 8
+               }
        }
 
-       for i > 0 {
-               numBytes++
-               i >>= 8
+       if i < 0 {
+               for i < -128 {
+                       numBytes++
+                       i >>= 8
+               }
        }
 
        return
index 67878f9bb936cabb6bedb51764a7a9d5a1358d32..492f39dace38fd8d6ad1455a575bdadc99e9c84b 100644 (file)
@@ -59,6 +59,10 @@ type marshalTest struct {
 
 var marshalTests = []marshalTest{
        marshalTest{10, "02010a"},
+       marshalTest{127, "02017f"},
+       marshalTest{128, "02020080"},
+       marshalTest{-128, "020180"},
+       marshalTest{-129, "0202ff7f"},
        marshalTest{intStruct{64}, "3003020140"},
        marshalTest{twoIntStruct{64, 65}, "3006020140020141"},
        marshalTest{nestedStruct{intStruct{127}}, "3005300302017f"},