Cover bounded reads and cancelled replacements
This commit is contained in:
@@ -3,6 +3,7 @@ package fileops
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
@@ -21,7 +22,11 @@ func ReadFileBounded(path string, limit int64) ([]byte, error) {
|
||||
return nil, fmt.Errorf("open file %q: %w", path, err)
|
||||
}
|
||||
defer func() { _ = file.Close() }()
|
||||
data, err := io.ReadAll(io.LimitReader(file, limit+1))
|
||||
readLimit := limit
|
||||
if limit < math.MaxInt64 {
|
||||
readLimit++
|
||||
}
|
||||
data, err := io.ReadAll(io.LimitReader(file, readLimit))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read file %q: %w", path, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user