You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by dn...@apache.org on 2015/10/26 11:32:30 UTC

svn commit: r1710552 - in /poi: site/src/documentation/content/xdocs/status.xml trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java

Author: dnorth
Date: Mon Oct 26 10:32:30 2015
New Revision: 1710552

URL: http://svn.apache.org/viewvc?rev=1710552&view=rev
Log:
Fix for https://bz.apache.org/bugzilla/show_bug.cgi?id=58549

Thanks to Damian Cugley for the patch.

Modified:
    poi/site/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java
    poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java

Modified: poi/site/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/site/src/documentation/content/xdocs/status.xml?rev=1710552&r1=1710551&r2=1710552&view=diff
==============================================================================
--- poi/site/src/documentation/content/xdocs/status.xml (original)
+++ poi/site/src/documentation/content/xdocs/status.xml Mon Oct 26 10:32:30 2015
@@ -50,6 +50,7 @@
         <action dev="PD" type="fix">Removed deprecated HDF API</action>
         <action dev="PD" type="fix" fixes-bug="58466">Improve column manipulation in XSSF to avoid changes overwriting each other</action>
         <action dev="PD" type="fix" fixes-bug="58471">Improve number formatting to more closely match Excel's behaviour</action>
+        <action dev="PD" type="fix" fixes-bug="58549">Fix row limits for HSSFClientAnchor</action>
     </release>
 
     <release version="3.13" date="2015-09-25">

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java?rev=1710552&r1=1710551&r2=1710552&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFClientAnchor.java Mon Oct 26 10:32:30 2015
@@ -19,6 +19,7 @@ package org.apache.poi.hssf.usermodel;
 
 import org.apache.poi.ddf.EscherClientAnchorRecord;
 import org.apache.poi.ddf.EscherRecord;
+import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.usermodel.ClientAnchor;
 
 /**
@@ -27,6 +28,9 @@ import org.apache.poi.ss.usermodel.Clien
  */
 public final class HSSFClientAnchor extends HSSFAnchor implements ClientAnchor {
 
+    public static final int MAX_COL = SpreadsheetVersion.EXCEL97.getLastColumnIndex();
+    public static final int MAX_ROW = SpreadsheetVersion.EXCEL97.getLastRowIndex();
+
     private EscherClientAnchorRecord _escherClientAnchor;
 
     public HSSFClientAnchor(EscherClientAnchorRecord escherClientAnchorRecord) {
@@ -63,15 +67,15 @@ public final class HSSFClientAnchor exte
         checkRange(dx2, 0, 1023, "dx2");
         checkRange(dy1, 0, 255, "dy1");
         checkRange(dy2, 0, 255, "dy2");
-        checkRange(col1, 0, 255, "col1");
-        checkRange(col2, 0, 255, "col2");
-        checkRange(row1, 0, 255 * 256, "row1");
-        checkRange(row2, 0, 255 * 256, "row2");
+        checkRange(col1, 0, MAX_COL, "col1");
+        checkRange(col2, 0, MAX_COL, "col2");
+        checkRange(row1, 0, MAX_ROW, "row1");
+        checkRange(row2, 0, MAX_ROW, "row2");
 
         setCol1((short) Math.min(col1, col2));
         setCol2((short) Math.max(col1, col2));
-        setRow1((short) Math.min(row1, row2));
-        setRow2((short) Math.max(row1, row2));
+        setRow1(Math.min(row1, row2));
+        setRow2(Math.max(row1, row2));
 
         if (col1 > col2){
             _isHorizontallyFlipped = true;
@@ -126,7 +130,7 @@ public final class HSSFClientAnchor exte
      * @param col1 the column(0 based) of the first cell.
      */
     public void setCol1(short col1) {
-        checkRange(col1, 0, 255, "col1");
+        checkRange(col1, 0, MAX_COL, "col1");
         _escherClientAnchor.setCol1(col1);
     }
 
@@ -148,7 +152,7 @@ public final class HSSFClientAnchor exte
      * @param col2 the column(0 based) of the second cell.
      */
     public void setCol2(short col2) {
-        checkRange(col2, 0, 255, "col2");
+        checkRange(col2, 0, MAX_COL, "col2");
         _escherClientAnchor.setCol2(col2);
     }
 
@@ -163,14 +167,14 @@ public final class HSSFClientAnchor exte
      * @return the row(0 based) of the first cell.
      */
     public int getRow1() {
-        return _escherClientAnchor.getRow1();
+        return unsignedValue(_escherClientAnchor.getRow1());
     }
 
     /**
      * @param row1 0-based row of the first cell.
      */
     public void setRow1(int row1) {
-        checkRange(row1, 0, 256 * 256, "row1");
+        checkRange(row1, 0, MAX_ROW, "row1");
         _escherClientAnchor.setRow1(Integer.valueOf(row1).shortValue());
     }
 
@@ -178,14 +182,14 @@ public final class HSSFClientAnchor exte
      * @return the row(0 based) of the second cell.
      */
     public int getRow2() {
-        return _escherClientAnchor.getRow2();
+        return unsignedValue(_escherClientAnchor.getRow2());
     }
 
     /**
      * @param row2 the row(0 based) of the second cell.
      */
     public void setRow2(int row2) {
-        checkRange(row2, 0, 256 * 256, "row2");
+        checkRange(row2, 0, MAX_ROW, "row2");
         _escherClientAnchor.setRow2(Integer.valueOf(row2).shortValue());
     }
 
@@ -211,10 +215,10 @@ public final class HSSFClientAnchor exte
         checkRange(getDx2(), 0, 1023, "dx2");
         checkRange(getDy1(), 0, 255, "dy1");
         checkRange(getDy2(), 0, 255, "dy2");
-        checkRange(getCol1(), 0, 255, "col1");
-        checkRange(getCol2(), 0, 255, "col2");
-        checkRange(getRow1(), 0, 255 * 256, "row1");
-        checkRange(getRow2(), 0, 255 * 256, "row2");
+        checkRange(getCol1(), 0, MAX_COL, "col1");
+        checkRange(getCol2(), 0, MAX_COL, "col2");
+        checkRange(getRow1(), 0, MAX_ROW, "row1");
+        checkRange(getRow2(), 0, MAX_ROW, "row2");
 
         setCol1(col1);
         setRow1(row1);
@@ -267,6 +271,16 @@ public final class HSSFClientAnchor exte
             throw new IllegalArgumentException(varName + " must be between " + minRange + " and " + maxRange + ", but was: " + value);
     }
 
+    /**
+     * Given a 16-bit unsigned integer stored in a short, return the unsigned value.
+     *
+     * @param s A 16-bit value intended to be interpreted as an unsigned integer.
+     * @return The value represented by <code>s</code>.
+     */
+    private static int unsignedValue(final short s) {
+        return (s < 0 ? 0x10000 + s : s);
+    }
+
     @Override
     public boolean equals(Object obj) {
         if (obj == null)

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java?rev=1710552&r1=1710551&r2=1710552&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFClientAnchor.java Mon Oct 26 10:32:30 2015
@@ -99,4 +99,63 @@ public final class TestHSSFClientAnchor
             assertEquals(ref[i], height, 0);
         }
     }
+
+    /**
+     * Check {@link HSSFClientAnchor} constructor does not treat 32768 as -32768.
+     */
+    public void testCanHaveRowGreaterThan32767() {
+        // Maximum permitted row number should be 65535.
+        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 32768, (short) 0, 32768);
+
+        assertEquals(32768, anchor.getRow1());
+        assertEquals(32768, anchor.getRow2());
+    }
+
+    /**
+     * Check the maximum is not set at 255*256 instead of 256*256 - 1.
+     */
+    public void testCanHaveRowUpTo65535() {
+        HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 65535, (short) 0, 65535);
+
+        assertEquals(65535, anchor.getRow1());
+        assertEquals(65535, anchor.getRow2());
+    }
+
+    public void testCannotHaveRowGreaterThan65535() {
+        try {
+            new HSSFClientAnchor(0, 0, 0, 0, (short) 0, 65536, (short) 0, 65536);
+            fail("Expected IllegalArgumentException to be thrown");
+        } catch (IllegalArgumentException ex) {
+            // pass
+        }
+    }
+
+    /**
+     * Check the same maximum value enforced when using {@link HSSFClientAnchor#setRow1}.
+     */
+    public void testCanSetRowUpTo65535() {
+        HSSFClientAnchor anchor = new HSSFClientAnchor();
+        anchor.setRow1(65535);
+        anchor.setRow2(65535);
+
+        assertEquals(65535, anchor.getRow1());
+        assertEquals(65535, anchor.getRow2());
+    }
+
+    public void testCannotSetRow1GreaterThan65535() {
+        try {
+            new HSSFClientAnchor().setRow1(65536);
+            fail("Expected IllegalArgumentException to be thrown");
+        } catch (IllegalArgumentException ex) {
+            // pass
+        }
+    }
+    public void testCannotSetRow2GreaterThan65535() {
+        try {
+            new HSSFClientAnchor().setRow2(65536);
+            fail("Expected IllegalArgumentException to be thrown");
+        } catch (IllegalArgumentException ex) {
+            // pass
+        }
+    }
 }



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