From 419f88a9c3a790af42cafa52a6306ff17291971c Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Mon, 30 Jun 2025 12:45:51 +0200 Subject: [PATCH] Fix incorrect line counts due to empty lines in CSV importer (#2542) --- internal/subimporter/importer.go | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go index 761dae27..79206576 100644 --- a/internal/subimporter/importer.go +++ b/internal/subimporter/importer.go @@ -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) {