Skip to content

XML syntax error in stream writer #1519

@IAkumaI

Description

@IAkumaI

Description

We have this line: https://github.com/qax-os/excelize/blob/master/cell.go#L495 which escapes text for xml via xml.EscapeText.
Than buff.String() goes to trimCellValue function which has this https://github.com/qax-os/excelize/blob/master/cell.go#L462

if len(value) > TotalCellChars {
	value = value[:TotalCellChars]
}

Because of how xml.EscapeText works, it makes string much more sometimes (for example, if we put html into it).
Than value[:TotalCellChars] cuts this escaped xml without care of tags.

So, if final escaped XML has ending like this ">" and overflows TotalCellChars by 1 or 2, out XML becomes invalid. It will have invalid unclosed tag.

Steps to reproduce the issue:

value := strings.Repeat("<>", TotalCellChars/2)
f := NewFile()
streamWriter, _ := f.NewStreamWriter("Sheet1")
_ = streamWriter.SetRow("A1", []interface{}{value}) // No errors
_ = streamWriter.Flush() // Still no errors

_, err := f.GetCellValue("Sheet1", "A1") // XML syntax error: invalid character entity &gt (no semicolon)

Describe the results you received:

invalid character entity &gt (no semicolon)

Describe the results you expected:

It works.

Output of go version:

go version go1.19.1 darwin/arm64

Excelize version or commit ID:

v2.7.1

Environment details (OS, Microsoft Excel™ version, physical, etc.):
MacOS Ventura

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmedThis issue can be reproduced

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions