mirror of
https://github.com/libp2p/go-libp2p-core.git
synced 2024-12-26 23:30:27 +08:00
648dc3fba2
* feat: harden encoding/decoding functions against panics Part of https://github.com/libp2p/go-libp2p/issues/1389 These kinds of functions: 1. Handle user input. 2. Often have out-of-bounds, null pointer, etc bugs. 3. Have completely isolated logic where local panics are unlikely to cause memory corruption elsewhere. * test: add a panic catcher test
19 lines
354 B
Go
19 lines
354 B
Go
package catch
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"runtime/debug"
|
|
)
|
|
|
|
var panicWriter io.Writer = os.Stderr
|
|
|
|
// HandlePanic handles and logs panics.
|
|
func HandlePanic(rerr interface{}, err *error, where string) {
|
|
if rerr != nil {
|
|
fmt.Fprintf(panicWriter, "caught panic: %s\n%s\n", rerr, debug.Stack())
|
|
*err = fmt.Errorf("panic in %s: %s", where, rerr)
|
|
}
|
|
}
|