Skip to content

Commit

Permalink
fix: update
Browse files Browse the repository at this point in the history
Signed-off-by: Junjie Gao <[email protected]>
  • Loading branch information
JeyJeyGao committed Dec 9, 2024
1 parent 0076d0f commit 32ae375
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
9 changes: 7 additions & 2 deletions internal/io/limitedwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@

package io

import "io"
import (
"errors"
"io"
)

var ErrLimitExceeded = errors.New("write limit exceeded")

// LimitedWriter is a writer that writes to an underlying writer up to a limit.
type LimitedWriter struct {
Expand All @@ -36,7 +41,7 @@ func LimitWriter(w io.Writer, limit int64) *LimitedWriter {
// Write writes p to the underlying writer up to the limit.
func (l *LimitedWriter) Write(p []byte) (int, error) {
if l.N <= 0 {
return 0, io.ErrShortWrite
return 0, ErrLimitExceeded
}
if int64(len(p)) > l.N {
p = p[:l.N]
Expand Down
6 changes: 3 additions & 3 deletions internal/io/limitedwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package io

import (
"bytes"
"io"
"errors"
"testing"
)

Expand Down Expand Up @@ -60,8 +60,8 @@ func TestLimitWriterFailed(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
_, err = lw.Write([]byte(longString))
expectedErr := io.ErrShortWrite
if err != expectedErr {
expectedErr := errors.New("write limit exceeded")
if err.Error() != expectedErr.Error() {
t.Errorf("expected error %v, got %v", expectedErr, err)
}
}
2 changes: 2 additions & 0 deletions plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ func (c execCommander) Output(ctx context.Context, name string, command plugin.C
var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, name, string(command))
cmd.Stdin = bytes.NewReader(req)
// The limit writer will be handled by the caller in run() by comparing the
// bytes written with the expected length of the bytes.
cmd.Stderr = io.LimitWriter(&stderr, maxPluginOutputSize)
cmd.Stdout = io.LimitWriter(&stdout, maxPluginOutputSize)
err := cmd.Run()
Expand Down

0 comments on commit 32ae375

Please sign in to comment.