sqids / sqids-go

Official Go port of Sqids. Generate short unique IDs from numbers.
https://sqids.org/go
MIT License
492 stars 8 forks source link

`math.MaxUint64` is not itself a `uint64` value #5

Closed peterhellberg closed 10 months ago

peterhellberg commented 11 months ago

As illustrated by this example:

https://go.dev/play/p/tckABU8iLbW

package main

import (
    "fmt"
    "math"
)

func MaxValue() uint64 {
    return math.MaxUint64
}

func main() {
    fmt.Println(MaxValue()+1 > MaxValue())
}
false

Adding 1 to the maximum value of a 64 bit unsigned integer in Go will result in it wrapping around, something the current code does not seem to take into consideration in code such as:

if numbers[0]+1 > MaxValue() {
    return "", errors.New("ran out of range checking against the blocklist")
}
4kimov commented 11 months ago

Uh oh, good catch. How do we fix that?

peterhellberg commented 10 months ago

Will be "solved" by not doing additions past the maximum uint64 value in the library after #10