diff options
author | Georg Pfuetzenreuter | 2024-10-10 02:33:42 +0200 |
---|---|---|
committer | Georg Pfuetzenreuter | 2024-10-10 18:43:46 +0200 |
commit | 930839ab58a60a5e3026de6f8a659fdcd8c25173 (patch) | |
tree | e4a9814487f76716c063511ff5379c3647a05463 /wat/utils.go | |
parent | 91b0e21b7ac29d11d7c6774c92c99f4640a1a8d1 (diff) | |
download | watbot-930839ab58a60a5e3026de6f8a659fdcd8c25173.tar.gz watbot-930839ab58a60a5e3026de6f8a659fdcd8c25173.tar.bz2 watbot-930839ab58a60a5e3026de6f8a659fdcd8c25173.zip |
Prevent dice overflow
rand.Int() would panic when the max value is <= 0, which happens when
big.NewInt() was fed with a too large number. Avoid this by validating
the big.NewInt() return beforehand. Add error handling to all callers to
both gracefully return to IRC and to log an error message.
Rename the shadowed "max" variable whilst at it to avoid confusion.
Signed-off-by: Georg Pfuetzenreuter <mail@georg-pfuetzenreuter.net>
Diffstat (limited to 'wat/utils.go')
-rw-r--r-- | wat/utils.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/wat/utils.go b/wat/utils.go new file mode 100644 index 0000000..331f7ad --- /dev/null +++ b/wat/utils.go @@ -0,0 +1,22 @@ +package wat + +import ( + "fmt" + "runtime" +) + +func handleError(err error) string { + if err != nil { + pc, _, _, ok := runtime.Caller(1) + details := runtime.FuncForPC(pc) + var cFun string + if ok && details != nil { + cFun = details.Name() + } else { + cFun = "???" + } + fmt.Printf("caught error in %s: %v\n", cFun, err) + return "u wat" + } + return "" +} |