diff --git a/pkg/yqlib/decoder_xml.go b/pkg/yqlib/decoder_xml.go index c34209e2..99626bd3 100644 --- a/pkg/yqlib/decoder_xml.go +++ b/pkg/yqlib/decoder_xml.go @@ -299,6 +299,10 @@ func (dec *xmlDecoder) decodeXML(root *xmlNode) error { log.Debug("chardata [%v] for %v", elem.n.Data, elem.label) } case xml.EndElement: + if elem == nil { + log.Debug("no element, probably bad xml") + continue + } log.Debug("end element %v", elem.label) elem.state = "finished" // And add it to its parent list diff --git a/pkg/yqlib/xml_test.go b/pkg/yqlib/xml_test.go index 8194f38c..b1620a0d 100644 --- a/pkg/yqlib/xml_test.go +++ b/pkg/yqlib/xml_test.go @@ -263,6 +263,12 @@ const expectedXmlWithProcInstAndDirectives = ` ` var xmlScenarios = []formatScenario{ + { + skipDoc: true, + description: "bad xml", + input: ``, + expected: "+p_xml: version=\"1.0\" encoding=\"UTF-8\"\n", + }, { skipDoc: true, input: " value ",