Bug 68232 - formatValue(StringBuffer,Object)@CellTextFormatter can have an index out of range issue
Summary: formatValue(StringBuffer,Object)@CellTextFormatter can have an index out of r...
Status: NEEDINFO
Alias: None
Product: POI
Classification: Unclassified
Component: SS Common (show other bugs)
Version: 5.3.x-dev
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-11-27 02:25 UTC by zhonghao
Modified: 2023-11-27 10:31 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description zhonghao 2023-11-27 02:25:18 UTC
The code is as follows:
  public void formatValue(StringBuffer toAppendTo, Object obj) {
        int start = toAppendTo.length();
        String text = obj.toString();
        if (obj instanceof Boolean) {
            text = text.toUpperCase(Locale.ROOT);
        }
        toAppendTo.append(desc);
        for (int textPo : textPos) {
            int pos = start + textPo;
            toAppendTo.replace(pos, pos + 1, text);
        }
    }

NPOI fixed a bug:

https://github.com/nissl-lab/npoi/commit/c546978db6bb703464ec9c58e8697b3f91709a86

The buggy code of NPOI is as follows:

 public override void FormatValue(StringBuilder toAppendTo, Object obj){...
   for (int i = 0; i < textPos.Length; i++)
            {
                int pos = start + textPos[i];
                //toAppendTo.Replace(pos, pos + 1, text);
                toAppendTo.Remove(pos, 1).Insert(pos, text);
            }
}
The fixed code is as follows:
 public override void FormatValue(StringBuilder toAppendTo, Object obj){...
    toAppendTo.Replace("\0", text);
}
Comment 1 PJ Fanning 2023-11-27 10:31:56 UTC
We are not going to just copy NPOI. Please provide real world examples. POI is a volunteer project and it is normally best to provide a patch with a test case yourself.