From 9342b43c9507b2c625fffeaa7633e5c1d92552d7 Mon Sep 17 00:00:00 2001 From: Yo Chiang Date: Thu, 5 Mar 2020 19:45:49 +0800 Subject: [PATCH] bpmodify: fix os.Exit() shouldn't shadow panic() The `defer func() { os.Exit() }()` in main() method shadows panic(). Make the exit handler recover() from panic(), log the panic(), and then gracefully exit. Test: m bpmodify Change-Id: Icc89f8fce0b6096489baa0ba0f08c21d1ef623bc --- bpmodify/bpmodify.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/bpmodify/bpmodify.go b/bpmodify/bpmodify.go index 13811a4..29e97d1 100644 --- a/bpmodify/bpmodify.go +++ b/bpmodify/bpmodify.go @@ -223,7 +223,12 @@ func walkDir(path string) { } func main() { - defer func() { os.Exit(exitCode) }() + defer func() { + if err := recover(); err != nil { + report(fmt.Errorf("error: %s", err)) + } + os.Exit(exitCode) + }() flag.Parse()