html: avoid invalid nil pointer access

Updates golang/go#23071

Change-Id: I73d7302c5bde4441aa824093fdcce52e8bb51e31
Reviewed-on: https://go-review.googlesource.com/107379
Run-TryBot: Kunpei Sakai <namusyaka@gmail.com>
Reviewed-by: Nigel Tao <nigeltao@golang.org>
This commit is contained in:
Kunpei Sakai
2018-04-17 07:06:43 +09:00
committed by Nigel Tao
parent 3121141a29
commit 8d16fa6dc9
2 changed files with 6 additions and 1 deletions

View File

@@ -2270,7 +2270,7 @@ func ParseFragment(r io.Reader, context *Node) ([]*Node, error) {
}
p.doc.AppendChild(root)
p.oe = nodeStack{root}
if context.DataAtom == a.Template {
if context != nil && context.DataAtom == a.Template {
p.templateStack = append(p.templateStack, inTemplateIM)
}
p.resetInsertionMode()

View File

@@ -380,6 +380,11 @@ func TestNodeConsistency(t *testing.T) {
}
}
func TestParseFragmentWithNilContext(t *testing.T) {
// This shouldn't panic.
ParseFragment(strings.NewReader("<p>hello</p>"), nil)
}
func BenchmarkParser(b *testing.B) {
buf, err := ioutil.ReadFile("testdata/go1.html")
if err != nil {