Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/utils/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
8 changes: 5 additions & 3 deletions pkg/utils/calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading