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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] - Correctly return the checked out buffer to the pool #2732

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions pkg/writers/buffered_file_writer/bufferedfilewriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ func (w *BufferedFileWriter) CloseForWriting() error {
return nil
}

// Return the buffer to the pool since the contents have been written to the file and
// the writer is transitioning to read-only mode.
w.bufPool.Put(w.buf)
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this free up the buffer for use elsewhere? That seems incompatible with reading from it below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Whoops, this was meant to be in a defer. I also clearly missed the failing test which would've also caught this. 馃槗


if w.buf.Len() > 0 {
_, err := w.buf.WriteTo(w.file)
if err != nil {
Expand Down
31 changes: 31 additions & 0 deletions pkg/writers/buffered_file_writer/bufferedfilewriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/trufflesecurity/trufflehog/v3/pkg/context"
"github.com/trufflesecurity/trufflehog/v3/pkg/writers/buffer"
)

func TestBufferedFileWriterNewThreshold(t *testing.T) {
Expand Down Expand Up @@ -506,3 +507,33 @@ func BenchmarkBufferedFileWriterWriteSmall(b *testing.B) {
rc.Close()
}
}

func TestBufferWriterCloseForWritingWithFile(t *testing.T) {
bufPool := buffer.NewBufferPool()

ctx := context.Background()
buf := bufPool.Get(ctx)
writer := &BufferedFileWriter{
threshold: 10,
bufPool: bufPool,
buf: buf,
}

// Write data exceeding the threshold to ensure a file is created.
data := []byte("this is a longer string exceeding the threshold")
_, err := writer.Write(data)
assert.NoError(t, err)

err = writer.CloseForWriting()
assert.NoError(t, err)
assert.Equal(t, readOnly, writer.state)

rdr, err := writer.ReadCloser()
assert.NoError(t, err)
defer rdr.Close()

// Get a buffer from the pool and check if it is the same buffer used in the writer.
bufFromPool := bufPool.Get(ctx)
assert.Same(t, buf, bufFromPool, "Buffer should be returned to the pool")
bufPool.Put(bufFromPool)
}
Loading