[Bug 63057] New: *Cell.setCellValue(String/RichTextString) is not exception-safe

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Bug 63057] New: *Cell.setCellValue(String/RichTextString) is not exception-safe

Bugzilla from bugzilla@apache.org
https://bz.apache.org/bugzilla/show_bug.cgi?id=63057

            Bug ID: 63057
           Summary: *Cell.setCellValue(String/RichTextString) is not
                    exception-safe
           Product: POI
           Version: 4.0.x-dev
          Hardware: PC
            Status: NEW
          Severity: normal
          Priority: P2
         Component: SS Common
          Assignee: [hidden email]
          Reporter: [hidden email]
  Target Milestone: ---

In all three implementations of Cell, in setCellValue(String) and
setCellValue(RichTextString) cell is convertyed to STRING before all checks
have passed (or failed). That is, if the passed string exceeds the maximum
length defined by the spreadsheet version, the method wil throw but the cell
contents will have changed.

The fix is simple: perform all checks before any invasive operation.

testcase:

@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()); // <- fails
    assertEquals(value, cell.getNumericCellValue(), 0); // <- fails, obviously
}

--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[Bug 63057] *Cell.setCellValue(String/RichTextString) is not exception-safe

Bugzilla from bugzilla@apache.org
https://bz.apache.org/bugzilla/show_bug.cgi?id=63057

[hidden email] <[hidden email]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 OS|                            |All

--- Comment #1 from [hidden email] <[hidden email]> ---
My bad, this only applies to SXSSFCell.

Also this in setCellValue(String):
if(_value.getType()==CellType.FORMULA)
  if(_value instanceof NumericFormulaValue) {
    ((NumericFormulaValue)
_value).setPreEvaluatedValue(Double.parseDouble(value));
  }
  ...
}

never happens because it is preceded by resetting the internal value to a
[Formula]StringValue, so I removed it.

--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

[Bug 63057] *Cell.setCellValue(String/RichTextString) is not exception-safe

Bugzilla from bugzilla@apache.org
In reply to this post by Bugzilla from bugzilla@apache.org
https://bz.apache.org/bugzilla/show_bug.cgi?id=63057

[hidden email] <[hidden email]> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #2 from [hidden email] <[hidden email]> ---
Fixed in r1850342

--
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]