poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1686689 - in /poi/trunk/src: java/org/apache/poi/hssf/record/NameRecord.java testcases/org/apache/poi/hssf/record/TestNameRecord.java
Date Sun, 21 Jun 2015 06:56:46 GMT
Author: centic
Date: Sun Jun 21 06:56:45 2015
New Revision: 1686689

URL: http://svn.apache.org/r1686689
Log:
Bug 57923: Fix check for tokens in NameRecord.getExternSheetNumber() to avoid ArrayOutOfBoundsException

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java?rev=1686689&r1=1686688&r2=1686689&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/NameRecord.java Sun Jun 21 06:56:45 2015
@@ -411,11 +411,12 @@ public final class NameRecord extends Co
 	 * @return extern sheet index
 	 */
 	public int getExternSheetNumber(){
-		if (field_13_name_definition.getEncodedSize() < 1) {
+	    Ptg[] tokens = field_13_name_definition.getTokens();
+		if (tokens.length == 0) {
 			return 0;
 		}
-		Ptg ptg = field_13_name_definition.getTokens()[0];
 
+		Ptg ptg = tokens[0];
 		if (ptg.getClass() == Area3DPtg.class){
 			return ((Area3DPtg) ptg).getExternSheetIndex();
 

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java?rev=1686689&r1=1686688&r2=1686689&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/TestNameRecord.java Sun Jun 21 06:56:45
2015
@@ -17,15 +17,19 @@
 
 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.HSSFWorkbook;
+import org.apache.poi.ss.formula.ptg.Area3DPtg;
 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.Ref3DPtg;
 import org.apache.poi.util.HexRead;
 
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
-
 /**
  * Tests the NameRecord serializes/deserializes correctly
  * 
@@ -60,7 +64,8 @@ public final class TestNameRecord extend
 		byte[] data2 = nr.serialize();
 		TestcaseRecordInputStream.confirmRecordEncoding(NameRecord.sid, data, data2);
 	}
-	public void testFormulaRelAbs_bug46174() {
+
+	public void testFormulaRelAbs_bug46174() throws IOException {
 		// perhaps this testcase belongs on TestHSSFName
 		HSSFWorkbook wb = new HSSFWorkbook();
 		HSSFName name = wb.createName();
@@ -68,11 +73,13 @@ public final class TestNameRecord extend
 		name.setNameName("test");
 		name.setRefersToFormula("Sheet1!$B$3");
 		if (name.getRefersToFormula().equals("Sheet1!B3")) {
-			throw new AssertionFailedError("Identified bug 46174");
+			fail("Identified bug 46174");
 		}
 		assertEquals("Sheet1!$B$3", name.getRefersToFormula());
+	    wb.close();
 	}
-	public void testFormulaGeneral() {
+
+	public void testFormulaGeneral() throws IOException {
 		// perhaps this testcase belongs on TestHSSFName
 		HSSFWorkbook wb = new HSSFWorkbook();
 		HSSFName name = wb.createName();
@@ -82,6 +89,8 @@ public final class TestNameRecord extend
 		assertEquals("Sheet1!A1+Sheet1!A2", name.getRefersToFormula());
 		name.setRefersToFormula("5*6");
 		assertEquals("5*6", name.getRefersToFormula());
+		
+		wb.close();
 	}
 
     /**
@@ -687,4 +696,22 @@ public final class TestNameRecord extend
         assertEquals("1.T20.001", vals[0][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());
+    }
+    
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message