go-xmlfmt/xmlfmt

Comments are still not parsed properly [strings: negative Repeat count]

bang9211 opened this issue · 1 comments

Hi @AntonioSun, I updated to 7fd73a9 but It still happened...

package main

import "github.com/go-xmlfmt/xmlfmt"

func main() {
	msg := `<!-- 무조건 착신전환 조회 -->
	<?xml version="1.0" encoding="utf-8"?>
	<message name="DIS_USER_SSVC" tid="1591918441">
	  <!-- 결과 코드 : 0(성공) / 그 외(실패) -->
	  <Result>0</Result>
	  <parameter>
		<SsvcList>
		  <!-- 가입/사용 여부 : 3(가입+사용 중) / 2(가입+미사용) -->
		  <CFU>2</CFU>
		  <!-- 착신 번호(CFU 가 3인 경우 사용) -->
		  <!--  <DATA>0261216281</DATA>  -->
		</SsvcList>
	  </parameter>
	</message>`
	xmlfmt.FormatXML(string(msg), "", "\t")
}

trace is

panic: strings: negative Repeat count

goroutine 1 [running]:
strings.Repeat({0x104e14937, 0x1}, 0xffffffffffffffff)
        /usr/local/go/src/strings/strings.go:529 +0x41c
github.com/go-xmlfmt/xmlfmt.replaceTag.func1({0x1400015019f, 0xa})
        /Users/hanyoungtak/Documents/go/pkg/mod/github.com/go-xmlfmt/xmlfmt@v0.0.0-20211206191508-7fd73a941850/xmlfmt.go:69 +0x24c
regexp.(*Regexp).ReplaceAllStringFunc.func1({0x1400014e300, 0x1d7, 0x300}, {0x140001260c0, 0x2, 0x2})
        /usr/local/go/src/regexp/regexp.go:596 +0x78
regexp.(*Regexp).replaceAll(0x14000118140, {0x0, 0x0, 0x0}, {0x14000150000, 0x1a9}, 0x2, 0x1400012fe58)
        /usr/local/go/src/regexp/regexp.go:634 +0x314
regexp.(*Regexp).ReplaceAllStringFunc(0x14000118140, {0x14000150000, 0x1a9}, 0x14000106270)
        /usr/local/go/src/regexp/regexp.go:595 +0x74
github.com/go-xmlfmt/xmlfmt.FormatXML({0x104e1eb08, 0x1d9}, {0x0, 0x0}, {0x104e14937, 0x1}, {0x0, 0x0, 0x0})
        /Users/hanyoungtak/Documents/go/pkg/mod/github.com/go-xmlfmt/xmlfmt@v0.0.0-20211206191508-7fd73a941850/xmlfmt.go:38 +0x144
main.main()
        /Users/hanyoungtak/Documents/go/src/xml-test/main.go:22 +0x54
exit status 2

the blow line caused the error.

<!--  <DATA>0261216281</DATA>  -->

from #2 #3

Please read:
https://pkg.go.dev/github.com/go-xmlfmt/xmlfmt?utm_source=godoc#FormatXML

"If your XML Comments have nested tags in them, ..."

It is also in the updated README:

// If the XML Comments have nested tags in them...

and also tested in

xmlfmt/xmlfmt_test.go

Lines 153 to 178 in 7fd73a9

<!-- c1 --> <?xml version="1.0" encoding="utf-8"?> <message name="DIS_USER_SSVC" tid="1591918441"> <!-- c2 --> <Result>0</Result> <parameter> <SsvcList> <!-- c3 --> <CFU>2</CFU> <!-- c4 --> <!-- <DATA>0261216281</DATA> --> </SsvcList> </parameter> </message>`
const wc1 = `
<book>
<author>Fred
</author>
<!-- <price>20</price><currency>USD</currency> -->
<isbn>23456
</isbn>
</book>
<!-- c1 -->
<?xml version="1.0" encoding="utf-8"?>
<message name="DIS_USER_SSVC" tid="1591918441">
<!-- c2 -->
<Result>0
</Result>
<parameter>
<SsvcList>
<!-- c3 -->
<CFU>2
</CFU>
<!-- c4 -->
<!-- <DATA>0261216281</DATA> -->
</SsvcList>
</parameter>
</message>`