From d688a1d9ebbeac1dba1e51863d480640e420ae55 Mon Sep 17 00:00:00 2001 From: SAY-5 Date: Tue, 12 May 2026 11:22:06 -0700 Subject: [PATCH] fix: avoid panic formatting processing instructions without attributes FormatXml indexed attrComponents[1] unconditionally when splitting a processing instruction's pseudo-attributes on '=', causing an index out of range panic for instructions like or any PI whose body is empty or lacks an '=' separator. --- internal/utils/utils.go | 14 ++++++++++---- internal/utils/utils_test.go | 1 + test/data/xml/formatted17.xml | 8 ++++++++ test/data/xml/unformatted17.xml | 4 ++++ 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 test/data/xml/formatted17.xml create mode 100644 test/data/xml/unformatted17.xml diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 2597b2a..ea7b479 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -94,10 +94,16 @@ func FormatXml(reader io.Reader, writer io.Writer, indent string, colors int) er _, _ = fmt.Fprintf(writer, "%s%s", tagColor(""), newline) diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go index b0ffd78..b3116ea 100644 --- a/internal/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -39,6 +39,7 @@ func TestFormatXml(t *testing.T) { "unformatted14.xml": "formatted14.xml", "unformatted15.xml": "formatted15.xml", "unformatted16.xml": "formatted16.xml", + "unformatted17.xml": "formatted17.xml", } for unformattedFile, expectedFile := range files { diff --git a/test/data/xml/formatted17.xml b/test/data/xml/formatted17.xml new file mode 100644 index 0000000..56af9ae --- /dev/null +++ b/test/data/xml/formatted17.xml @@ -0,0 +1,8 @@ + + + + + + Test + + diff --git a/test/data/xml/unformatted17.xml b/test/data/xml/unformatted17.xml new file mode 100644 index 0000000..37933bc --- /dev/null +++ b/test/data/xml/unformatted17.xml @@ -0,0 +1,4 @@ + + + +Test