Bug 64244

Summary: [PATCH] Fix exception from using applyFont for unicode representation _xHHHH_
Product: POI Reporter: Hyeonmin Park <hyeonmin.park>
Component: XSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major CC: hyeonmin.park
Priority: P2    
Version: 4.1.2-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Add testUtfDecode_withApplyFont and patch XSSFRichTextString.getFormatMap

Description Hyeonmin Park 2020-03-19 13:39:50 UTC
Created attachment 37107 [details]
Add testUtfDecode_withApplyFont and patch XSSFRichTextString.getFormatMap

If you use `applyFont()` for `XSSFRichString` having unicode representation _xHHHH_ format, an IllegalArgumentException is thrown. `testUtfDecode_withApplyFont` is a new test made by myself, also in the patch.

> Text length was 8 but the last format index was 20
> java.lang.IllegalArgumentException: Text length was 8 but the last format index was 20
> 	at org.apache.poi.xssf.usermodel.XSSFRichTextString.buildCTRst(XSSFRichTextString.java:590)
> 	at org.apache.poi.xssf.usermodel.XSSFRichTextString.applyFont(XSSFRichTextString.java:157)
> 	at org.apache.poi.xssf.usermodel.XSSFRichTextString.applyFont(XSSFRichTextString.java:167)
> 	at org.apache.poi.xssf.usermodel.TestXSSFRichTextString.testUtfDecode_withApplyFont(TestXSSFRichTextString.java:269)
> 	...

So I modified `XSSFRichTextString.getFormatMap()` to sum length after utf-decoded. Then the test and all tests are passed well.

Actually, I have an original xlsx file that makes this problem, but due to the security reason, I can't share the file. I think someone can help making MWE.
Comment 1 Dominik Stadler 2020-03-24 11:00:59 UTC
Fixed via r1875571, I ended up adding a more optimized utfLength() method which avoids constructing the full replaced string when only the resulting length is required.
Comment 2 Hyeonmin Park 2020-03-24 11:29:56 UTC
Thanks a lot, and looking forward to release soon. :)
Comment 3 Hyeonmin Park 2021-01-21 05:19:44 UTC
Suggestion: add this ticket to History of Changes http://poi.apache.org/changes.html#5.0.0
Comment 4 Dominik Stadler 2021-01-23 10:11:42 UTC
The changelog is now updated with some more entries that were missing.