Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
This commit is contained in:
Georg Pfuetzenreuter 2024-09-28 20:41:47 +02:00
commit bf11085207
Signed by: Georg
GPG Key ID: 1ED2F138E7E6FF57
4 changed files with 50 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
go-overflow-sample

18
README.md Normal file
View File

@ -0,0 +1,18 @@
```
$ go build
$ ./go-overflow-sample 999999999999999999
1492596583345867513
$ ./go-overflow-sample 9999999999999999999
Could not convert argument to int64
panic: crypto/rand: argument to Int is <= 0
goroutine 1 [running]:
crypto/rand.Int({0x4d0b88?, 0xc000014060?}, 0x0?)
/usr/lib64/go/1.22/src/crypto/rand/util.go:64 +0x2ca
main.RandInt(0x4d0b08?)
/home/georg/Work/git/go-overflow-sample/main.go:23 +0x86
main.main()
/home/georg/Work/git/go-overflow-sample/main.go:18 +0xdc
```

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module go-overflow-sample
go 1.22.6

28
main.go Normal file
View File

@ -0,0 +1,28 @@
package main
import "crypto/rand"
import "math/big"
import "fmt"
import "os"
import "strconv"
func main() {
if len(os.Args) < 2 {
fmt.Println("Pass a number")
os.Exit(1)
}
number, err := strconv.Atoi(os.Args[1])
if err != nil {
fmt.Println("Could not convert argument to int64")
}
RandInt(int64(number * 100))
}
func RandInt(max int64) {
i, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
fmt.Println("Could not randomize")
}
fmt.Println(i.Uint64())
}