From: Vladislav Galas Date: Fri, 4 Jan 2019 10:10:30 +0000 (+0000) Subject: Bug 63057: made SXSSFCell.setCellValue(String|RichTextString) exception-safe. X-Git-Tag: REL_4_1_0~123 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1f9b858ae5b41aef1f599c2f47de519d1e36be30;p=poi.git Bug 63057: made SXSSFCell.setCellValue(String|RichTextString) exception-safe. Also removed unused code. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1850342 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java index ed95d88b21..8917e60105 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFCell.java @@ -274,19 +274,17 @@ public class SXSSFCell implements Cell { @Override public void setCellValue(RichTextString value) { - XSSFRichTextString xvalue = (XSSFRichTextString)value; - - if (xvalue != null && xvalue.getString() != null) { - ensureRichTextStringType(); - - if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { + if (value != null && value.getString() != null) { + if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); } + ensureRichTextStringType(); + if(_value instanceof RichTextStringFormulaValue) { - ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(xvalue); + ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(value); } else { - ((RichTextValue) _value).setValue(xvalue); + ((RichTextValue) _value).setValue(value); } } else { setCellType(CellType.BLANK); @@ -305,22 +303,17 @@ public class SXSSFCell implements Cell { public void setCellValue(String value) { if (value != null) { - ensureTypeOrFormulaType(CellType.STRING); - if (value.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) { throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters"); } - - if(_value.getType()==CellType.FORMULA) - if(_value instanceof NumericFormulaValue) { - ((NumericFormulaValue) _value).setPreEvaluatedValue(Double.parseDouble(value)); - } else if(_value instanceof RichTextStringFormulaValue) { - ((RichTextStringFormulaValue) _value).setPreEvaluatedValue(new XSSFRichTextString(value)); - } else { - ((StringFormulaValue) _value).setPreEvaluatedValue(value); - } - else - ((PlainStringValue)_value).setValue(value); + + ensureTypeOrFormulaType(CellType.STRING); + + if(_value.getType() == CellType.FORMULA) { + ((StringFormulaValue) _value).setPreEvaluatedValue(value); + } else { + ((PlainStringValue) _value).setValue(value); + } } else { setCellType(CellType.BLANK); } diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java index eab4222ba3..a699161410 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestCell.java @@ -1156,4 +1156,47 @@ public abstract class BaseTestCell { boolean result = cell.getBooleanCellValue(); assertFalse(result); } + + @Test + public void setStringCellValue_ifThrows_shallNotChangeCell() { + Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); + + final double value = 2.78; + cell.setCellValue(value); + assertEquals(CellType.NUMERIC, cell.getCellType()); + + int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1; + String badStringValue = new String(new byte[badLength]); + + try { + cell.setCellValue(badStringValue); + } catch (IllegalArgumentException e) { + // no-op, expected to throw but we need to assert something more + } + + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertEquals(value, cell.getNumericCellValue(), 0); + } + + @Test + public void setStringCellValueWithRichTextString_ifThrows_shallNotChangeCell() { + Cell cell = _testDataProvider.createWorkbook().createSheet().createRow(0).createCell(0); + + final double value = 2.78; + cell.setCellValue(value); + assertEquals(CellType.NUMERIC, cell.getCellType()); + + int badLength = cell.getSheet().getWorkbook().getSpreadsheetVersion().getMaxTextLength() + 1; + RichTextString badStringValue = cell.getSheet().getWorkbook().getCreationHelper(). + createRichTextString(new String(new byte[badLength])); + + try { + cell.setCellValue(badStringValue); + } catch (IllegalArgumentException e) { + // no-op, expected to throw but we need to assert something more + } + + assertEquals(CellType.NUMERIC, cell.getCellType()); + assertEquals(value, cell.getNumericCellValue(), 0); + } }