Bug 49841

Summary: LEFT and RIGHT text functions throw a StringIndexOutOfBoundsException when called with a negative operand
Product: POI Reporter: Brendan Nolan <brendan.nolan>
Component: HSSFAssignee: POI Developers List <dev>
Status: RESOLVED FIXED    
Severity: major    
Priority: P2    
Version: 3.7-dev   
Target Milestone: ---   
Hardware: PC   
OS: All   
Attachments: Patch containing fix and test case

Description Brendan Nolan 2010-08-29 16:04:45 UTC
LEFT("ANYSTRING", -1)
RIGHT("ANYSTRING", -1)

Either of the functions above should return #VALUE! but return an exception

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
	at java.lang.String.substring(String.java:1937)
	at org.apache.poi.hssf.record.formula.functions.TextFunction$LeftRight.evaluate(TextFunction.java:183)
	at org.apache.poi.hssf.record.formula.functions.Var1or2ArgFunction.evaluate(Var1or2ArgFunction.java:36)
	at org.apache.poi.ss.formula.OperationEvaluatorFactory.evaluate(OperationEvaluatorFactory.java:132)


HSSFWorkbook wbHSSF = new HSSFWorkbook();
HSSFSheet sheetHSSF = wbHSSF.createSheet("LEFTRIGHTTESTSHEET");
FormulaEvaluator createFormulaEvaluator = wbHSSF.getCreationHelper().createFormulaEvaluator();
HSSFCell createCell = sheetHSSF.createRow(0).createCell(0,Cell.CELL_TYPE_FORMULA);
createCell.setCellFormula("LEFT(\"ANYSTRING\",-1)");		
createFormulaEvaluator.evaluate(createCell);
Comment 1 Brendan Nolan 2010-08-29 16:11:33 UTC
Created attachment 25959 [details]
Patch containing fix and test case
Comment 2 Yegor Kozlov 2010-09-04 05:48:59 UTC
Applied in r992570

Thanks,
Yegor