jaswdr / faker

:rocket: Ultimate fake data generator for Go with zero dependencies
https://pkg.go.dev/github.com/jaswdr/faker
MIT License
549 stars 59 forks source link

faker: fix panic from int wraparound when Intn(math.MaxInt) #160

Closed sean- closed 8 months ago

sean- commented 8 months ago

Description

When rand.Intn()'s arg wraps to a negative number, rand.Intn() panics. When faker.IntBetween(0, math.MaxInt) is used, diff is already MaxInt, so diff+1 causes an integer wraparound and rand.Intn() panics.

Also use math.MaxInt and math.MinInt constants.

Go Version

$ go version
go version go1.21.5 darwin/arm64

Go Tests

$ go test
➜ go test
2023/12/27 16:17:12 Error while requesting https://loremflickr.com/300/200 : request failed
2023/12/27 16:17:12 Error while creating a temp file: temp file creation failed
2023/12/27 16:17:12 Error while requesting https://randomuser.me : request failed
2023/12/27 16:17:12 Error while creating a temp file: temp file creation failed
PASS
ok      github.com/jaswdr/faker 4.731s
jaswdr commented 8 months ago

@sean- I took hover your PR and merged it in #163 applying the fixes to the comments I made. Thank you again for pointing this out, and congratulations for your first PR :tada: