-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbounded_buffer.go
More file actions
71 lines (59 loc) · 1.96 KB
/
bounded_buffer.go
File metadata and controls
71 lines (59 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package hostlib
import (
"bytes"
)
// DefaultMaxOutputSize is the default limit for stdout/stderr from exec commands (10MB).
// Prevents excessive memory usage from long-running commands with verbose output.
const DefaultMaxOutputSize = 10 * 1024 * 1024
// DefaultMaxRequestSize limits the size of incoming requests (1MB).
// This prevents malicious WASM modules from triggering OOM by claiming huge request sizes.
const DefaultMaxRequestSize = 1 * 1024 * 1024
// BoundedBuffer is a bytes.Buffer wrapper that limits the size of written data.
// It implements io.Writer and can be used with exec.Cmd's Stdout/Stderr.
type BoundedBuffer struct {
buffer bytes.Buffer
limit int
Truncated bool
}
// NewBoundedBuffer creates a new BoundedBuffer with the specified limit.
func NewBoundedBuffer(limit int) *BoundedBuffer {
return &BoundedBuffer{
limit: limit,
}
}
// Write implements io.Writer.
// It writes data up to the limit and then silently discards any additional data.
// The Truncated field is set to true if any data was discarded.
func (b *BoundedBuffer) Write(p []byte) (n int, err error) {
if b.buffer.Len() >= b.limit {
b.Truncated = true
return len(p), nil // Pretend we wrote it all to satisfy io.Writer contract
}
remaining := b.limit - b.buffer.Len()
if len(p) > remaining {
b.Truncated = true
n, err = b.buffer.Write(p[:remaining])
if err != nil {
return n, err
}
return len(p), nil // Return len(p) to avoid short write error
}
return b.buffer.Write(p)
}
// String returns the buffer contents as a string.
func (b *BoundedBuffer) String() string {
return b.buffer.String()
}
// Bytes returns the buffer contents as a byte slice.
func (b *BoundedBuffer) Bytes() []byte {
return b.buffer.Bytes()
}
// Len returns the current length of the buffer.
func (b *BoundedBuffer) Len() int {
return b.buffer.Len()
}
// Reset resets the buffer and clears the Truncated flag.
func (b *BoundedBuffer) Reset() {
b.buffer.Reset()
b.Truncated = false
}