Fix incorrect line counts due to empty lines in CSV importer (#2542)

This commit is contained in:
Adam Howard 2025-06-30 12:45:51 +02:00 committed by GitHub
parent aa864fd5bf
commit 419f88a9c3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -707,23 +707,32 @@ func (s *Session) mapCSVHeaders(csvHdrs []string, knownHdrs map[string]bool) map
// Credit: https://stackoverflow.com/a/24563853
func countLines(r io.Reader) (int, error) {
var (
buf = make([]byte, 32*1024)
count = 0
lineSep = []byte{'\n'}
buf = make([]byte, 32*1024)
count = 0
lineSep = byte('\n')
lastByte byte
)
for {
c, err := r.Read(buf)
count += bytes.Count(buf[:c], lineSep)
if c > 0 {
count += bytes.Count(buf[:c], []byte{lineSep})
lastByte = buf[c-1]
}
switch {
case err == io.EOF:
return count, nil
case err != nil:
if err == io.EOF {
break
}
if err != nil {
return count, err
}
}
if lastByte != 0 && lastByte != lineSep {
count++
}
return count, nil
}
func makeDomainMap(domains []string) (map[string]struct{}, bool) {