From 2f0661558883e60e148d319d89401d56870a9756 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 24 Sep 2012 10:30:37 -0400 Subject: [PATCH] encoding/asn1: prepare for 64-bit ints The code was assuming that int = 32 bits. Don't. Update #2188. R=agl CC=golang-dev https://golang.org/cl/6543063 --- src/pkg/encoding/asn1/asn1.go | 33 +++++++++++++++--------------- src/pkg/encoding/asn1/asn1_test.go | 2 +- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/pkg/encoding/asn1/asn1.go b/src/pkg/encoding/asn1/asn1.go index ac2b5f8daa6..cac9d64b5e9 100644 --- a/src/pkg/encoding/asn1/asn1.go +++ b/src/pkg/encoding/asn1/asn1.go @@ -77,15 +77,15 @@ func parseInt64(bytes []byte) (ret int64, err error) { // parseInt treats the given bytes as a big-endian, signed integer and returns // the result. -func parseInt(bytes []byte) (int, error) { +func parseInt32(bytes []byte) (int32, error) { ret64, err := parseInt64(bytes) if err != nil { return 0, err } - if ret64 != int64(int(ret64)) { + if ret64 != int64(int32(ret64)) { return 0, StructuralError{"integer too large"} } - return int(ret64), nil + return int32(ret64), nil } var bigOne = big.NewInt(1) @@ -670,7 +670,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam err = err1 return case enumeratedType: - parsedInt, err1 := parseInt(innerBytes) + parsedInt, err1 := parseInt32(innerBytes) if err1 == nil { v.SetInt(int64(parsedInt)) } @@ -692,19 +692,20 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam } err = err1 return - case reflect.Int, reflect.Int32: - parsedInt, err1 := parseInt(innerBytes) - if err1 == nil { - val.SetInt(int64(parsedInt)) + case reflect.Int, reflect.Int32, reflect.Int64: + if val.Type().Size() == 4 { + parsedInt, err1 := parseInt32(innerBytes) + if err1 == nil { + val.SetInt(int64(parsedInt)) + } + err = err1 + } else { + parsedInt, err1 := parseInt64(innerBytes) + if err1 == nil { + val.SetInt(parsedInt) + } + err = err1 } - err = err1 - return - case reflect.Int64: - parsedInt, err1 := parseInt64(innerBytes) - if err1 == nil { - val.SetInt(parsedInt) - } - err = err1 return // TODO(dfc) Add support for the remaining integer types case reflect.Struct: diff --git a/src/pkg/encoding/asn1/asn1_test.go b/src/pkg/encoding/asn1/asn1_test.go index eb848bdb4af..cabdf03b482 100644 --- a/src/pkg/encoding/asn1/asn1_test.go +++ b/src/pkg/encoding/asn1/asn1_test.go @@ -64,7 +64,7 @@ var int32TestData = []int32Test{ func TestParseInt32(t *testing.T) { for i, test := range int32TestData { - ret, err := parseInt(test.in) + ret, err := parseInt32(test.in) if (err == nil) != test.ok { t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok) }