From 4bd783d732d61dba2a6fb6b9c1f9f6f2c08cdc82 Mon Sep 17 00:00:00 2001 From: Matthew Chum Date: Mon, 10 Aug 2020 17:54:00 -0400 Subject: [PATCH 1/2] Add variable bit support --- src/encoding/asn1/asn1.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index d809dde2781cd8..e7be91d23cec01 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -22,7 +22,6 @@ package asn1 import ( "errors" "fmt" - "math" "math/big" "reflect" "strconv" @@ -305,9 +304,9 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) offset = initOffset var ret64 int64 for shifted := 0; offset < len(bytes); shifted++ { - // 5 * 7 bits per byte == 35 bits of data - // Thus the representation is either non-minimal or too large for an int32 - if shifted == 5 { + // n shifted bytes * 7 bits per byte should be less than the max number of bits + // Thus the representation is either non-minimal or too large for the system + if shifted * 7 > strconv.IntSize { err = StructuralError{"base 128 integer too large"} return } @@ -324,7 +323,7 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) if b&0x80 == 0 { ret = int(ret64) // Ensure that the returned value fits in an int on all platforms - if ret64 > math.MaxInt32 { + if ret64 > int64(^uint(0) >> 1) { err = StructuralError{"base 128 integer too large"} } return From 55e7f5b29745f5d2c6690630086970a6ea795165 Mon Sep 17 00:00:00 2001 From: Matthew Chum Date: Wed, 12 Aug 2020 15:00:33 -0400 Subject: [PATCH 2/2] clarifying comment on bit size --- src/encoding/asn1/asn1.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/encoding/asn1/asn1.go b/src/encoding/asn1/asn1.go index e7be91d23cec01..0aea14179e7ac5 100644 --- a/src/encoding/asn1/asn1.go +++ b/src/encoding/asn1/asn1.go @@ -304,8 +304,9 @@ func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) offset = initOffset var ret64 int64 for shifted := 0; offset < len(bytes); shifted++ { - // n shifted bytes * 7 bits per byte should be less than the max number of bits - // Thus the representation is either non-minimal or too large for the system + // n shifted bytes * 7 bits per byte should be less than or equal to the max number + // of bits. If greater, the representation is either non-minimal or too large for + // the platform if shifted * 7 > strconv.IntSize { err = StructuralError{"base 128 integer too large"} return