Skip to content

Commit

Permalink
check numchunks and chunk length are not 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ian-shim committed Mar 10, 2025
1 parent c062d54 commit 6251faf
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions encoding/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package encoding
import (
"errors"
"fmt"
"math"

"golang.org/x/exp/constraints"
)
Expand All @@ -11,8 +12,6 @@ var (
ErrInvalidParams = errors.New("invalid encoding params")
)

const maxUint64 = ^uint64(0)

type EncodingParams struct {
ChunkLength uint64 // ChunkSize is the length of the chunk in symbols
NumChunks uint64
Expand Down Expand Up @@ -63,7 +62,14 @@ func GetNumSys(dataSize uint64, chunkLen uint64) uint64 {

// ValidateEncodingParams takes in the encoding parameters and returns an error if they are invalid.
func ValidateEncodingParams(params EncodingParams, SRSOrder uint64) error {
if params.ChunkLength != 0 && params.NumChunks > maxUint64/params.ChunkLength {
if params.NumChunks == 0 {
return errors.New("number of chunks must be greater than 0")
}
if params.ChunkLength == 0 {
return errors.New("chunk length must be greater than 0")
}

if params.NumChunks > math.MaxUint64/params.ChunkLength {
return fmt.Errorf("multiplication overflow: ChunkLength: %d, NumChunks: %d", params.ChunkLength, params.NumChunks)
}

Expand Down

0 comments on commit 6251faf

Please # to comment.