Cover bounded reads and cancelled replacements

This commit is contained in:
2026-09-02 01:19:21 +00:00
parent 9f84446509
commit d67a0389e0
4 changed files with 104 additions and 8 deletions

View File

@@ -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)
}