Skip to content

Commit 419f88a

Browse files
authored
Fix incorrect line counts due to empty lines in CSV importer (#2542)
1 parent aa864fd commit 419f88a

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

internal/subimporter/importer.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -707,23 +707,32 @@ func (s *Session) mapCSVHeaders(csvHdrs []string, knownHdrs map[string]bool) map
707707
// Credit: https://stackoverflow.com/a/24563853
708708
func countLines(r io.Reader) (int, error) {
709709
var (
710-
buf = make([]byte, 32*1024)
711-
count = 0
712-
lineSep = []byte{'\n'}
710+
buf = make([]byte, 32*1024)
711+
count = 0
712+
lineSep = byte('\n')
713+
lastByte byte
713714
)
714715

715716
for {
716717
c, err := r.Read(buf)
717-
count += bytes.Count(buf[:c], lineSep)
718-
719-
switch {
720-
case err == io.EOF:
721-
return count, nil
718+
if c > 0 {
719+
count += bytes.Count(buf[:c], []byte{lineSep})
720+
lastByte = buf[c-1]
721+
}
722722

723-
case err != nil:
723+
if err == io.EOF {
724+
break
725+
}
726+
if err != nil {
724727
return count, err
725728
}
726729
}
730+
731+
if lastByte != 0 && lastByte != lineSep {
732+
count++
733+
}
734+
735+
return count, nil
727736
}
728737

729738
func makeDomainMap(domains []string) (map[string]struct{}, bool) {

0 commit comments

Comments
 (0)