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:
parent
6efa648853
commit
2f06615588
@ -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:
|
||||||
|
@ -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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user