cmd/digraph: use ReadString rather than bufio.Scanner

if an input line is too long (more than bufio.MaxScanTokenSize),
bufio.Scanner returns bufio.ErrToolong.

Use bufio's ReadString instead.

Fixes https://github.com/golang/go/issues/57807
This commit is contained in:
Fumitoshi Ukai 2023-01-17 15:35:12 +09:00
Родитель 8e94967361
Коммит e7057d029e
2 изменённых файлов: 16 добавлений и 6 удалений

Просмотреть файл

@ -351,20 +351,29 @@ func parse(rd io.Reader) (graph, error) {
g := make(graph)
var linenum int
in := bufio.NewScanner(rd)
for in.Scan() {
// We avoid bufio.Scanner as it imposes a (configurable) limit
// on line length, whereas Reader.ReadString does not.
in := bufio.NewReader(rd)
for {
linenum++
line, err := in.ReadString('\n')
eof := false
if err == io.EOF {
eof = true
} else if err != nil {
return nil, err
}
// Split into words, honoring double-quotes per Go spec.
words, err := split(in.Text())
words, err := split(line)
if err != nil {
return nil, fmt.Errorf("at line %d: %v", linenum, err)
}
if len(words) > 0 {
g.addEdges(words[0], words[1:]...)
}
}
if err := in.Err(); err != nil {
return nil, err
if eof {
break
}
}
return g, nil
}

Просмотреть файл

@ -45,6 +45,7 @@ e e
{"scss", g2, "sccs", nil, "c d\ne\n"},
{"scc", g2, "scc", []string{"d"}, "c\nd\n"},
{"succs", g2, "succs", []string{"a"}, "b\nc\n"},
{"succs-long-token", g2 + "x " + strings.Repeat("x", 96*1024), "succs", []string{"x"}, strings.Repeat("x", 96*1024) + "\n"},
{"preds", g2, "preds", []string{"c"}, "a\nd\n"},
{"preds multiple args", g2, "preds", []string{"c", "d"}, "a\nb\nc\nd\n"},
} {