From 930839ab58a60a5e3026de6f8a659fdcd8c25173 Mon Sep 17 00:00:00 2001 From: Georg Pfuetzenreuter Date: Thu, 10 Oct 2024 02:33:42 +0200 Subject: 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 --- wat/utils.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 wat/utils.go (limited to 'wat/utils.go') 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 "" +} -- cgit v1.2.3