Integer overflow prevention is wrong
Author: decorator-factoryCreated Feb 10, 2025Updated Dec 9, 2025
Labelserratum
func AddInt(a, b int) int {
if a > math.MaxInt-b {
panic("int overflow")
}
return a + b
}This function panics if a is positive and b is negative.
func main() {
AddInt(100, -20) // panic
}Here's an example of correctly handling overflow/underflow: https://github.com/shansec/go_code/blob/61331ebda07df6573065a1baf4fdc032884c43f6/microservices/addsrv/service.go#L40-L42
Source: teivah/100-go-mistakes