diff --git a/pkg/utils/bytes.go b/pkg/utils/bytes.go index cfcccdc..6fd1ca8 100644 --- a/pkg/utils/bytes.go +++ b/pkg/utils/bytes.go @@ -15,5 +15,5 @@ func Uint64ToBytes(i uint64) []byte { // to byteSlice will also modify the contents of the string, so it is the caller's responsibility // to ensure that the byte slice will not modified after the string is created. func UnsafeCastToString(byteSlice []byte) string { - return *(*string)(unsafe.Pointer(&byteSlice)) // #nosec G103 + return unsafe.String(unsafe.SliceData(byteSlice), len(byteSlice)) } diff --git a/pkg/utils/calc.go b/pkg/utils/calc.go index 541f8c6..382463d 100644 --- a/pkg/utils/calc.go +++ b/pkg/utils/calc.go @@ -7,14 +7,16 @@ import ( // CalculateMaxSize calculates the maximum size of a message that can be sent over the network. // add some extra bytes for header and service info -// check possible overflow also +// check possible overflow also using integer arithmetic func CalculateMaxSize(src int64) (int, error) { if src <= 0 { return 0, fmt.Errorf("src must be positive: %d", src) } - finalVal := src + int64(float64(src)*0.2) - if finalVal < 0 || finalVal > math.MaxInt { + maxAllowed := int64(math.MaxInt) + overhead := src / 5 + if src > maxAllowed-overhead { return 0, fmt.Errorf("src is too large, final value not fit: %d", src) } + finalVal := src + overhead return int(finalVal), nil }