Bug 57923: Fix check for tokens in NameRecord.getExternSheetNumber() to avoid ArrayOutOfBoundsException

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1686689 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2015-06-21 06:56:45 +00:00
parent e484eeac43
commit f58646196d
2 changed files with 36 additions and 8 deletions

View File

@ -411,11 +411,12 @@ public final class NameRecord extends ContinuableRecord {
* @return extern sheet index * @return extern sheet index
*/ */
public int getExternSheetNumber(){ public int getExternSheetNumber(){
if (field_13_name_definition.getEncodedSize() < 1) { Ptg[] tokens = field_13_name_definition.getTokens();
if (tokens.length == 0) {
return 0; return 0;
} }
Ptg ptg = field_13_name_definition.getTokens()[0];
Ptg ptg = tokens[0];
if (ptg.getClass() == Area3DPtg.class){ if (ptg.getClass() == Area3DPtg.class){
return ((Area3DPtg) ptg).getExternSheetIndex(); return ((Area3DPtg) ptg).getExternSheetIndex();

View File

@ -17,15 +17,19 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.poi.hssf.usermodel.HSSFName; import org.apache.poi.hssf.usermodel.HSSFName;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.ptg.Area3DPtg;
import org.apache.poi.ss.formula.ptg.ArrayPtg; import org.apache.poi.ss.formula.ptg.ArrayPtg;
import org.apache.poi.ss.formula.ptg.NamePtg;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.Ref3DPtg;
import org.apache.poi.util.HexRead; import org.apache.poi.util.HexRead;
import junit.framework.AssertionFailedError;
import junit.framework.TestCase;
/** /**
* Tests the NameRecord serializes/deserializes correctly * Tests the NameRecord serializes/deserializes correctly
* *
@ -60,7 +64,8 @@ public final class TestNameRecord extends TestCase {
byte[] data2 = nr.serialize(); byte[] data2 = nr.serialize();
TestcaseRecordInputStream.confirmRecordEncoding(NameRecord.sid, data, data2); TestcaseRecordInputStream.confirmRecordEncoding(NameRecord.sid, data, data2);
} }
public void testFormulaRelAbs_bug46174() {
public void testFormulaRelAbs_bug46174() throws IOException {
// perhaps this testcase belongs on TestHSSFName // perhaps this testcase belongs on TestHSSFName
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFName name = wb.createName(); HSSFName name = wb.createName();
@ -68,11 +73,13 @@ public final class TestNameRecord extends TestCase {
name.setNameName("test"); name.setNameName("test");
name.setRefersToFormula("Sheet1!$B$3"); name.setRefersToFormula("Sheet1!$B$3");
if (name.getRefersToFormula().equals("Sheet1!B3")) { if (name.getRefersToFormula().equals("Sheet1!B3")) {
throw new AssertionFailedError("Identified bug 46174"); fail("Identified bug 46174");
} }
assertEquals("Sheet1!$B$3", name.getRefersToFormula()); assertEquals("Sheet1!$B$3", name.getRefersToFormula());
wb.close();
} }
public void testFormulaGeneral() {
public void testFormulaGeneral() throws IOException {
// perhaps this testcase belongs on TestHSSFName // perhaps this testcase belongs on TestHSSFName
HSSFWorkbook wb = new HSSFWorkbook(); HSSFWorkbook wb = new HSSFWorkbook();
HSSFName name = wb.createName(); HSSFName name = wb.createName();
@ -82,6 +89,8 @@ public final class TestNameRecord extends TestCase {
assertEquals("Sheet1!A1+Sheet1!A2", name.getRefersToFormula()); assertEquals("Sheet1!A1+Sheet1!A2", name.getRefersToFormula());
name.setRefersToFormula("5*6"); name.setRefersToFormula("5*6");
assertEquals("5*6", name.getRefersToFormula()); assertEquals("5*6", name.getRefersToFormula());
wb.close();
} }
/** /**
@ -687,4 +696,22 @@ public final class TestNameRecord extends TestCase {
assertEquals("1.T20.001", vals[0][0]); assertEquals("1.T20.001", vals[0][0]);
assertEquals("1.T20.010", vals[vals.length - 1][0]); assertEquals("1.T20.010", vals[vals.length - 1][0]);
} }
public void testBug57923() {
NameRecord record = new NameRecord();
assertEquals(0, record.getExternSheetNumber());
record.setNameDefinition(new Ptg[] {});
assertEquals(0, record.getExternSheetNumber());
record.setNameDefinition(new Ptg[] {new NamePtg(1)});
assertEquals(0, record.getExternSheetNumber());
record.setNameDefinition(new Ptg[] {new Area3DPtg("area", 1)});
assertEquals(1, record.getExternSheetNumber());
record.setNameDefinition(new Ptg[] {new Ref3DPtg("A1", 1)});
assertEquals(1, record.getExternSheetNumber());
}
} }