Browse Source

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>
Kunpei Sakai 7 năm trước cách đây
mục cha
commit
8d16fa6dc9
2 tập tin đã thay đổi với 6 bổ sung1 xóa
  1. 1 1
      html/parse.go
  2. 5 0
      html/parse_test.go

+ 1 - 1
html/parse.go

@@ -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()

+ 5 - 0
html/parse_test.go

@@ -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 {