Skip to content

Commit

Permalink
benchfmt: return an error in case of a non UTF-8 encoded file
Browse files Browse the repository at this point in the history
Currently, when using the benchstat tool to read files that are not UTF-8 encoded, it will fail silently.
With this patch, when parsing non UTF-8 encoded files, benchfmt will now return an error instead letting the user know their input file is not properly formatted.

Fixes golang/go#58579
  • Loading branch information
phenpessoa committed Mar 13, 2023
1 parent f7320a6 commit 680867c
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions benchfmt/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ func (r *Reader) Scan() bool {
r.result.line++
// We do everything in byte buffers to avoid allocation.
line := r.s.Bytes()
// We only accept utf-8 encoded files.
if !utf8.Valid(line) {
r.err = fmt.Errorf("%s: invalid encode, only utf-8 encoded files are supported", r.result.fileName)
return false
}
// Most lines are benchmark lines, and we can check
// for that very quickly, so start with that.
if bytes.HasPrefix(line, benchmarkPrefix) {
Expand Down

0 comments on commit 680867c

Please sign in to comment.