Skip to content

Commit 5bedd86

Browse files
ukaiadonovan
authored andcommitted
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 golang.org/go#57807 Change-Id: If4165213668a468b844ee6a0ae84263475504208 GitHub-Last-Rev: e7057d0 GitHub-Pull-Request: golang#423 Reviewed-on: https://go-review.googlesource.com/c/tools/+/462053 Run-TryBot: Ian Lance Taylor <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent f6ea009 commit 5bedd86

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

cmd/digraph/digraph.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -351,20 +351,29 @@ func parse(rd io.Reader) (graph, error) {
351351
g := make(graph)
352352

353353
var linenum int
354-
in := bufio.NewScanner(rd)
355-
for in.Scan() {
354+
// We avoid bufio.Scanner as it imposes a (configurable) limit
355+
// on line length, whereas Reader.ReadString does not.
356+
in := bufio.NewReader(rd)
357+
for {
356358
linenum++
359+
line, err := in.ReadString('\n')
360+
eof := false
361+
if err == io.EOF {
362+
eof = true
363+
} else if err != nil {
364+
return nil, err
365+
}
357366
// Split into words, honoring double-quotes per Go spec.
358-
words, err := split(in.Text())
367+
words, err := split(line)
359368
if err != nil {
360369
return nil, fmt.Errorf("at line %d: %v", linenum, err)
361370
}
362371
if len(words) > 0 {
363372
g.addEdges(words[0], words[1:]...)
364373
}
365-
}
366-
if err := in.Err(); err != nil {
367-
return nil, err
374+
if eof {
375+
break
376+
}
368377
}
369378
return g, nil
370379
}

cmd/digraph/digraph_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ e e
4545
{"scss", g2, "sccs", nil, "c d\ne\n"},
4646
{"scc", g2, "scc", []string{"d"}, "c\nd\n"},
4747
{"succs", g2, "succs", []string{"a"}, "b\nc\n"},
48+
{"succs-long-token", g2 + "x " + strings.Repeat("x", 96*1024), "succs", []string{"x"}, strings.Repeat("x", 96*1024) + "\n"},
4849
{"preds", g2, "preds", []string{"c"}, "a\nd\n"},
4950
{"preds multiple args", g2, "preds", []string{"c", "d"}, "a\nb\nc\nd\n"},
5051
} {

0 commit comments

Comments
 (0)