Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover general chunker panics #3625

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/sources/chunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sources
import (
"bufio"
"errors"
"fmt"
"io"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
Expand Down Expand Up @@ -99,6 +100,22 @@ func readInChunks(ctx context.Context, reader io.Reader, config *chunkReaderConf
go func() {
defer close(chunkResultChan)

// Defer a panic recovery to handle any panics that occur while reading, which can sometimes unavoidably happen
// due to third-party library bugs.
defer func() {
if r := recover(); r != nil {
var panicErr error
if e, ok := r.(error); ok {
panicErr = e
} else {
panicErr = fmt.Errorf("panic occurred: %v", r)
}
chunkResultChan <- ChunkResult{
err: fmt.Errorf("panic error: %w", panicErr),
}
}
}()

for {
chunkRes := ChunkResult{}
chunkBytes := make([]byte, config.totalSize)
Expand Down
17 changes: 17 additions & 0 deletions pkg/sources/chunker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sources

import (
"bytes"
"io"
"math/rand"
"runtime"
"strings"
Expand All @@ -10,6 +11,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
)
Expand Down Expand Up @@ -121,6 +123,21 @@ func TestNewChunkedReader(t *testing.T) {
}
}

type panicReader struct{}

var _ io.Reader = (*panicReader)(nil)

func (_ panicReader) Read([]byte) (int, error) {
panic("panic for testing")
}

func TestChunkReader_UnderlyingReaderPanics_DoesNotPanic(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the test verify that the error is returned through the channel?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good question! my vote is "no," because imo the specific correct behavior is not obvious, beyond "shouldn't panic" - which is what the test checks right now.

require.NotPanics(t, func() {
for range NewChunkReader()(context.Background(), &panicReader{}) {
}
})
}

func BenchmarkChunkReader(b *testing.B) {
var bigChunk = make([]byte, 1<<24) // 16MB

Expand Down
Loading