-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: harden encoding/decoding functions against panics (#243)
* feat: harden encoding/decoding functions against panics Part of #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
- Loading branch information
Showing
11 changed files
with
154 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package catch | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCatch(t *testing.T) { | ||
buf := new(bytes.Buffer) | ||
|
||
oldPanicWriter := panicWriter | ||
t.Cleanup(func() { panicWriter = oldPanicWriter }) | ||
panicWriter = buf | ||
|
||
panicAndCatch := func() (err error) { | ||
defer func() { HandlePanic(recover(), &err, "somewhere") }() | ||
|
||
panic("here") | ||
} | ||
|
||
err := panicAndCatch() | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "panic in somewhere: here") | ||
|
||
require.Contains(t, buf.String(), "caught panic: here") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.