From 284f7d19c5409e321dd23880230afaaebc15d34a Mon Sep 17 00:00:00 2001 From: Javen O'Neal Date: Fri, 19 May 2017 08:26:41 +0000 Subject: [PATCH] github-55: NumberFormatException if XSSFName.setNameName is set with a long name that looks similar to a cell address. Thanks to Thomas S @Millie4Ever This closes #55 on github. https://github.com/apache/poi/pull/55 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1795595 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/xssf/usermodel/XSSFName.java | 10 ++++-- .../poi/xssf/usermodel/TestXSSFName.java | 33 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java index 1e97f8275..f72e10c0f 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFName.java @@ -402,8 +402,14 @@ public final class XSSFName implements Name { if (name.matches("[A-Za-z]+\\d+")) { String col = name.replaceAll("\\d", ""); String row = name.replaceAll("[A-Za-z]", ""); - if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL97)) { - throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); + + try { + if (CellReference.cellReferenceIsWithinRange(col, row, SpreadsheetVersion.EXCEL2007)) { + throw new IllegalArgumentException("Invalid name: '"+name+"': cannot be $A$1-style cell reference"); + } + } catch (final NumberFormatException e) { + // row was not parseable as an Integer, such as a BigInt + // therefore name passes the not-a-cell-reference criteria } } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java index a188a11ed..3989d74d6 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFName.java @@ -17,14 +17,16 @@ package org.apache.poi.xssf.usermodel; -import org.apache.poi.xssf.XSSFTestDataSamples; -import org.junit.Test; -import org.apache.poi.xssf.XSSFITestDataProvider; - import static org.junit.Assert.*; +import java.io.IOException; +import java.util.Arrays; +import org.junit.Test; + import org.apache.poi.ss.usermodel.BaseTestNamedRange; import org.apache.poi.ss.util.CellRangeAddress; +import org.apache.poi.xssf.XSSFTestDataSamples; +import org.apache.poi.xssf.XSSFITestDataProvider; /** * @author Yegor Kozlov @@ -130,4 +132,27 @@ public final class TestXSSFName extends BaseTestNamedRange { wb.close(); } + + //github-55 + @Test + public void testSetNameNameCellAddress() throws IOException { + XSSFWorkbook wb = new XSSFWorkbook(); + wb.createSheet("First Sheet"); + XSSFName name = wb.createName(); + + // Cell addresses/references are not allowed + for (String ref : Arrays.asList("A1", "$A$1", "A1:B2")) { + try { + name.setNameName(ref); + fail("cell addresses are not allowed: " + ref); + } catch (final IllegalArgumentException e) { + // expected + } + } + + // Name that looks similar to a cell reference but is outside the cell reference row and column limits + name.setNameName("A0"); + name.setNameName("F04030020010"); + name.setNameName("XFDXFD10"); + } }