celestiaorg / go-square

A library for encoding blobs into a 2D square of evenly sized chunks designed for sampling and reconstruction
Apache License 2.0
13 stars 16 forks source link

inclusion: RoundUpByMultipleOf and all other modulo denominator must be checked for zero before usage #45

Open odeke-em opened 6 months ago

odeke-em commented 6 months ago

If we examine https://github.com/celestiaorg/go-square/blob/4e84d80f3e55ac3093005c8089186d68ee62541d/inclusion/blob_share_commitment_rules.go#L44-L47 we see that an outside modulus is being used. Let's bullet proof by firstly checking if the denominator is zero and about a runtime panic of divide by zero

rootulp commented 6 months ago

Agreed. I think you're proposing something like:

// RoundUpByMultipleOf rounds cursor up to the next multiple of v. If cursor is divisible
// by v, then it returns cursor.
func RoundUpByMultipleOf(cursor, v int) (int, error) {
    if v == 0 {
        return 0, fmt.Errorf("v cannot be 0")
    }
    if cursor%v == 0 {
        return cursor, nil
    }
    return ((cursor / v) + 1) * v, nil
}

which is API breaking but IMO seems reasonable.