Bug 51273

Summary: ArrayIndexOutOfBoundsException in FormulaCellCacheEntrySet
Product: POI Reporter: Alex Mountney <amountney>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 3.8-dev   
Target Milestone: ---   
Hardware: Other   
OS: Linux   

Description Alex Mountney 2011-05-26 16:58:36 UTC
java.lang.ArrayIndexOutOfBoundsException: -2
        at org.apache.poi.ss.formula.FormulaCellCacheEntrySet.addInternal(FormulaCellCacheEntrySet.java:83)
        at org.apache.poi.ss.formula.FormulaCellCacheEntrySet.add(FormulaCellCacheEntrySet.java:72)
        at org.apache.poi.ss.formula.CellCacheEntry.addConsumingCell(CellCacheEntry.java:85)
        at org.apache.poi.ss.formula.FormulaCellCacheEntry.changeConsumingCells(FormulaCellCacheEntry.java:80)
        at org.apache.poi.ss.formula.FormulaCellCacheEntry.setSensitiveInputCells(FormulaCellCacheEntry.java:60)
        at org.apache.poi.ss.formula.FormulaCellCacheEntry.updateFormulaResult(FormulaCellCacheEntry.java:109)
        at org.apache.poi.ss.formula.CellEvaluationFrame.updateFormulaResult(CellEvaluationFrame.java:75)
        at org.apache.poi.ss.formula.EvaluationTracker.updateCacheResult(EvaluationTracker.java:94)
        at org.apache.poi.ss.formula.WorkbookEvaluator.evaluateAny(WorkbookEvaluator.java:286)
        at org.apache.poi.ss.formula.WorkbookEvaluator.evaluate(WorkbookEvaluator.java:221)
        at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluateFormulaCellValue(HSSFFormulaEvaluator.java:320)
        at org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator.evaluate(HSSFFormulaEvaluator.java:182)

Appears to occur randomly when calling HSSFFormulaEvaluator.evaluate(Cell cell) more than once on the same cell.

Looking at the FormulaCellCacheEntrySet.java:83:

	int startIx = cce.hashCode() % arr.length;
	for(int i=startIx; i<arr.length; i++) {
	
startIx could be negative hence the ArrayIndexOutOfBoundsException

Workaround: call HSSFFormulaEvaluator.clearAllCachedResultValues() before invoking evaluate on the same cell although suspect there would still be issues in a multithreaded environment.
Comment 1 Nick Burch 2011-05-27 11:46:37 UTC
Should be fixed in r1128268 - looks like there was a missing Math.abs

Note however that if you're updating cells after having done one calculation, you do need to tell the formula evaluator so it can tweak its cache.