Browse Source

Do not allow text longer than 32,767 characters in HSSF cells, see Bugzilla 47751

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@811994 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_3_5-FINAL
Yegor Kozlov 14 years ago
parent
commit
1559a6d4bd

+ 5
- 0
src/java/org/apache/poi/hssf/usermodel/HSSFCell.java View File

@@ -540,6 +540,11 @@ public class HSSFCell implements Cell {
setCellType(CELL_TYPE_BLANK, false, row, col, styleIndex);
return;
}

if(hvalue.length() > SpreadsheetVersion.EXCEL97.getMaxTextLength()){
throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
}

if (_cellType == CELL_TYPE_FORMULA) {
// Set the 'pre-evaluated result' for the formula
// note - formulas do not preserve text formatting.

+ 17
- 4
src/java/org/apache/poi/ss/SpreadsheetVersion.java View File

@@ -36,9 +36,11 @@ public enum SpreadsheetVersion {
* <li>The total number of available rows is 64k (2^16)</li>
* <li>The maximum number of arguments to a function is 30</li>
* <li>Number of conditional format conditions on a cell is 3</li>
* <li>Length of text cell contents is unlimited </li>
* <li>Length of text cell contents is 32767</li>
* </ul>
*/
EXCEL97(0x10000, 0x0100, 30, 3),
EXCEL97(0x10000, 0x0100, 30, 3, 32767),

/**
* Excel2007
@@ -49,21 +51,24 @@ public enum SpreadsheetVersion {
* <li>The maximum number of arguments to a function is 255</li>
* <li>Number of conditional format conditions on a cell is unlimited
* (actually limited by available memory in Excel)</li>
* <li>Length of text cell contents is unlimited </li>
* <ul>
*/
EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE);
EXCEL2007(0x100000, 0x4000, 255, Integer.MAX_VALUE, Integer.MAX_VALUE);

private final int _maxRows;
private final int _maxColumns;
private final int _maxFunctionArgs;
private final int _maxCondFormats;
private final int _maxTextLength;

private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats) {
private SpreadsheetVersion(int maxRows, int maxColumns, int maxFunctionArgs, int maxCondFormats, int maxText) {
_maxRows = maxRows;
_maxColumns = maxColumns;
_maxFunctionArgs = maxFunctionArgs;
_maxCondFormats = maxCondFormats;
}
_maxTextLength = maxText;
}

/**
* @return the maximum number of usable rows in each spreadsheet
@@ -116,4 +121,12 @@ public enum SpreadsheetVersion {
public String getLastColumnName() {
return CellReference.convertNumToColString(getLastColumnIndex());
}

/**
* @return the maximum length of a text cell
*/
public int getMaxTextLength() {
return _maxTextLength;
}

}

+ 16
- 1
src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java View File

@@ -38,7 +38,7 @@ import org.apache.poi.hssf.record.NameRecord;
import org.apache.poi.hssf.record.aggregates.FormulaRecordAggregate;
import org.apache.poi.hssf.record.formula.DeletedArea3DPtg;
import org.apache.poi.hssf.record.formula.Ptg;
import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.TempFile;

/**
@@ -1494,4 +1494,19 @@ public final class TestBugs extends BaseTestBugzillaIssues {
assertEquals(893, wb.getNumberOfNames());
assertEquals("Matthew\\Matthew11_1\\Matthew2331_1\\Matthew2351_1\\Matthew2361_1___lab", wb.getNameName(300));
}

/**
* HSSFRichTextString.length() returns negative for really long strings.
* The test file was created in OpenOffice 3.0 as Excel does not allow cell text longer than 32,767 characters
*/
public void test46368() {
HSSFWorkbook wb = openSample("46368.xls");
HSSFSheet s = wb.getSheetAt(0);
HSSFCell cell1 = s.getRow(0).getCell(0);
assertEquals(32770, cell1.getStringCellValue().length());

HSSFCell cell2 = s.getRow(2).getCell(0);
assertEquals(32766, cell2.getStringCellValue().length());
}

}

+ 32
- 0
src/testcases/org/apache/poi/hssf/usermodel/TestHSSFCell.java View File

@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;

import java.util.Date;
import java.util.GregorianCalendar;
import java.io.FileOutputStream;

import junit.framework.AssertionFailedError;

@@ -30,6 +31,8 @@ import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.StringRecord;
import org.apache.poi.ss.usermodel.BaseTestCell;
import org.apache.poi.ss.usermodel.ErrorConstants;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.SpreadsheetVersion;

/**
* Tests various functionality having to do with {@link HSSFCell}. For instance support for
@@ -291,5 +294,34 @@ public final class TestHSSFCell extends BaseTestCell {
Record dbcr = recs[index++];
assertEquals(DBCellRecord.class, dbcr.getClass());
}

/**
* The maximum length of cell contents (text) is 32,767 characters.
*/
public void testMaxTextLength(){
HSSFSheet sheet = new HSSFWorkbook().createSheet();
HSSFCell cell = sheet.createRow(0).createCell(0);

int maxlen = SpreadsheetVersion.EXCEL97.getMaxTextLength();
assertEquals(32767, maxlen);

StringBuffer b = new StringBuffer() ;

// 32767 is okay
for( int i = 0 ; i < maxlen ; i++ )
{
b.append( "X" ) ;
}
cell.setCellValue(b.toString());

b.append("X");
// 32768 produces an invalid XLS file
try {
cell.setCellValue(b.toString());
fail("Expected exception");
} catch (IllegalArgumentException e){
assertEquals("The maximum length of cell contents (text) is 32,767 characters", e.getMessage());
}
}
}


+ 0
- 32
src/testcases/org/apache/poi/ss/usermodel/BaseTestBugzillaIssues.java View File

@@ -218,38 +218,6 @@ public abstract class BaseTestBugzillaIssues extends TestCase {
assertTrue("no errors parsing formula", true);
}

/**
* HSSFRichTextString.length() returns negative for really
* long strings
*/
public void test46368() {
Workbook wb = getTestDataProvider().createWorkbook();
Sheet s = wb.createSheet();
Row r = s.createRow(0);
for(int i=0; i<15; i++) {
int len = 32760 + i;
Cell c = r.createCell(i);

StringBuffer sb = new StringBuffer();
for(int j=0; j<len; j++) {
sb.append("x");
}
RichTextString rtr = wb.getCreationHelper().createRichTextString(sb.toString());
assertEquals(len, rtr.length());
c.setCellValue(rtr);
}

// Save and reload
wb = getTestDataProvider().writeOutAndReadBack(wb);
s = wb.getSheetAt(0);
r = s.getRow(0);
for(int i=0; i<15; i++) {
int len = 32760 + i;
Cell c = r.getCell(i);
assertEquals(len, c.getRichStringCellValue().length());
}
}

public void test18800() {
Workbook book = getTestDataProvider().createWorkbook();
book.createSheet("TEST");

BIN
test-data/spreadsheet/46368.xls View File


Loading…
Cancel
Save