From ec9ce5de1f2e35cb2db36ea4970fed9bb4e13257 Mon Sep 17 00:00:00 2001 From: lazerg Date: Fri, 30 Oct 2026 17:09:52 +0500 Subject: [PATCH] html: don't panic on or and " parsed with a nil context failed with a nil pointer dereference. Check that the context is non-nil first. Fixes golang/go#82727 --- html/parse.go | 4 ++-- html/parse_test.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/html/parse.go b/html/parse.go index a99cee6653..c6c4bda672 100644 --- a/html/parse.go +++ b/html/parse.go @@ -1045,7 +1045,7 @@ func inBodyIM(p *parser) bool { p.acknowledgeSelfClosingTag() p.framesetOK = false case a.Input: - if p.fragment && p.context.DataAtom == a.Select { + if p.fragment && p.context != nil && p.context.DataAtom == a.Select { // Ignore the token. return true } @@ -1110,7 +1110,7 @@ func inBodyIM(p *parser) bool { // Don't let the tokenizer go into raw text mode when scripting is disabled. p.tokenizer.NextIsNotRawText() case a.Select: - if p.fragment && p.context.DataAtom == a.Select { + if p.fragment && p.context != nil && p.context.DataAtom == a.Select { // Ignore the token. return true } else if p.popUntil(defaultScope, a.Select) { diff --git a/html/parse_test.go b/html/parse_test.go index 734cd21848..6211893d4e 100644 --- a/html/parse_test.go +++ b/html/parse_test.go @@ -468,8 +468,17 @@ func TestNodeConsistency(t *testing.T) { } func TestParseFragmentWithNilContext(t *testing.T) { - // This shouldn't panic. - ParseFragment(strings.NewReader("

hello

"), nil) + srcs := []string{ + "

hello

", + "", + "