Skip to content

Commit

Permalink
add sanity checks to avoid panic in GetBitString when numBits is 0 (#296
Browse files Browse the repository at this point in the history
)

Signed-off-by: Tianchang <[email protected]>
  • Loading branch information
ty3gx authored Dec 9, 2024
1 parent 98b7213 commit 55579ff
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/asn1/aper/aper.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package aper
import (
"encoding/hex"
"fmt"
"reflect"

"github.com/onosproject/onos-lib-go/api/asn1/v1/asn1"
"github.com/onosproject/onos-lib-go/pkg/errors"
"github.com/onosproject/onos-lib-go/pkg/logging"
"reflect"
)

var log = logging.GetLogger("asn1", "aper")
Expand Down Expand Up @@ -39,6 +40,10 @@ func perBitLog(numBits uint64, byteOffset uint64, bitsOffset uint, value interfa

// GetBitString is to get BitString with desire size from source byte array with bit offset
func GetBitString(srcBytes []byte, bitsOffset uint, numBits uint) (dstBytes []byte, err error) {
if numBits == 0 {
return []byte{}, nil
}

bitsLeft := uint(len(srcBytes))*8 - bitsOffset
if numBits > bitsLeft {
err = fmt.Errorf("Get bits overflow, requireBits: %d, leftBits: %d", numBits, bitsLeft)
Expand Down

0 comments on commit 55579ff

Please sign in to comment.