Skip to content

Commit

Permalink
Fix buffer on ReadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanhecl committed Feb 13, 2022
1 parent 73afb81 commit 389685c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions goini.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@ func ReadFile(Path string) ([]string, error) {
return nil, err
}
defer f.Close()
var lines []string
buf := make([]byte, 32*1024)
var (
buf []byte = make([]byte, 32*1024)
lines []string
line []byte = []byte{}
)
for {
line := []byte{}
n, err := f.Read(buf)
if n > 0 {
for i := 0; i < n; i++ {
Expand All @@ -111,9 +113,6 @@ func ReadFile(Path string) ([]string, error) {
line = append(line, buf[i])
}
}
if len(line) > 0 {
lines = append(lines, string(line))
}
}
if err == io.EOF {
break
Expand All @@ -122,6 +121,9 @@ func ReadFile(Path string) ([]string, error) {
return nil, fmt.Errorf("read %d bytes: %v", n, err)
}
}
if len(line) > 0 {
lines = append(lines, string(line))
}
return lines, nil
}

Expand Down

0 comments on commit 389685c

Please sign in to comment.