Skip to content

Commit

Permalink
utils/file: fix check of max read size
Browse files Browse the repository at this point in the history
One byte matters! Quite possible to read exactly maxReadSize
bytes and pop up a truncation error, but in fact no truncation
has happened. This patch tidies up a truncation check by reading
+1 byte and replacing '==' on '>' comparison.

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
  • Loading branch information
rouming committed Dec 13, 2022
1 parent 967cd05 commit 2b40fe2
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/pillar/utils/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func ReadWithMaxSize(log *base.LogObject, filename string, maxReadSize int) ([]b
}
defer f.Close()
r := bufio.NewReader(f)
content := make([]byte, maxReadSize)
content := make([]byte, maxReadSize+1)
n, err := r.Read(content)
if err != nil {
err = fmt.Errorf("ReadWithMaxSize %s failed: %v", filename, err)
Expand All @@ -197,13 +197,14 @@ func ReadWithMaxSize(log *base.LogObject, filename string, maxReadSize int) ([]b
}
return nil, err
}
if n == maxReadSize {
if n > maxReadSize {
err = fmt.Errorf("ReadWithMaxSize %s truncated after %d bytes",
filename, maxReadSize)
n = maxReadSize
} else {
err = nil
}
return content[0:n], err
return content[:n], err
}

// ReadSavedCounter returns counter value from provided file
Expand Down

0 comments on commit 2b40fe2

Please sign in to comment.