1
0
mirror of https://github.com/golang/go synced 2024-11-20 10:34:42 -07:00

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
This commit is contained in:
Russ Cox 2012-09-24 10:30:37 -04:00
parent 6efa648853
commit 2f06615588
2 changed files with 18 additions and 17 deletions

View File

@ -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 // parseInt treats the given bytes as a big-endian, signed integer and returns
// the result. // the result.
func parseInt(bytes []byte) (int, error) { func parseInt32(bytes []byte) (int32, error) {
ret64, err := parseInt64(bytes) ret64, err := parseInt64(bytes)
if err != nil { if err != nil {
return 0, err return 0, err
} }
if ret64 != int64(int(ret64)) { if ret64 != int64(int32(ret64)) {
return 0, StructuralError{"integer too large"} return 0, StructuralError{"integer too large"}
} }
return int(ret64), nil return int32(ret64), nil
} }
var bigOne = big.NewInt(1) var bigOne = big.NewInt(1)
@ -670,7 +670,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
err = err1 err = err1
return return
case enumeratedType: case enumeratedType:
parsedInt, err1 := parseInt(innerBytes) parsedInt, err1 := parseInt32(innerBytes)
if err1 == nil { if err1 == nil {
v.SetInt(int64(parsedInt)) v.SetInt(int64(parsedInt))
} }
@ -692,19 +692,20 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
} }
err = err1 err = err1
return return
case reflect.Int, reflect.Int32: case reflect.Int, reflect.Int32, reflect.Int64:
parsedInt, err1 := parseInt(innerBytes) if val.Type().Size() == 4 {
if err1 == nil { parsedInt, err1 := parseInt32(innerBytes)
val.SetInt(int64(parsedInt)) 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 return
// TODO(dfc) Add support for the remaining integer types // TODO(dfc) Add support for the remaining integer types
case reflect.Struct: case reflect.Struct:

View File

@ -64,7 +64,7 @@ var int32TestData = []int32Test{
func TestParseInt32(t *testing.T) { func TestParseInt32(t *testing.T) {
for i, test := range int32TestData { for i, test := range int32TestData {
ret, err := parseInt(test.in) ret, err := parseInt32(test.in)
if (err == nil) != test.ok { if (err == nil) != test.ok {
t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok) t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
} }