From bcfa5a7efcaa1b6a359a23eb8fdd6f6c247d0ce3 Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Sun, 27 Jan 2008 14:55:32 +0000 Subject: [PATCH] fix bug #44297: IntPtg must operate with unsigned short. Reading signed short results in incorrect formula calculation. git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@615610 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 1 + src/documentation/content/xdocs/status.xml | 1 + .../poi/hssf/record/formula/IntPtg.java | 27 ++--- .../org/apache/poi/hssf/data/44297.xls | Bin 0 -> 14848 bytes .../poi/hssf/usermodel/TestBug44297.java | 103 ++++++++++++++++++ .../poi/hssf/model/TestFormulaParser.java | 5 +- 6 files changed, 116 insertions(+), 21 deletions(-) create mode 100755 src/scratchpad/testcases/org/apache/poi/hssf/data/44297.xls create mode 100755 src/scratchpad/testcases/org/apache/poi/hssf/usermodel/TestBug44297.java diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index d5f6e9c53..3e9214997 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -36,6 +36,7 @@ + 44297 - IntPtg must operate with unsigned short. Reading signed short results in incorrect formula calculation 44296 - Fix for reading slide background images 44293 - Avoid swapping AreaPtgs from relative to absolute 44292 - Correctly process the last paragraph in a word file diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 69190f915..dfe4e5aa6 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -33,6 +33,7 @@ + 44297 - IntPtg must operate with unsigned short. Reading signed short results in incorrect formula calculation 44296 - Fix for reading slide background images 44293 - Avoid swapping AreaPtgs from relative to absolute 44292 - Correctly process the last paragraph in a word file diff --git a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java index 602289776..257c089df 100644 --- a/src/java/org/apache/poi/hssf/record/formula/IntPtg.java +++ b/src/java/org/apache/poi/hssf/record/formula/IntPtg.java @@ -40,7 +40,7 @@ public class IntPtg { public final static int SIZE = 3; public final static byte sid = 0x1e; - private short field_1_value; + private int field_1_value; private IntPtg() { //Required for clone methods @@ -48,42 +48,31 @@ public class IntPtg public IntPtg(RecordInputStream in) { - setValue(in.readShort()); + setValue(in.readUShort()); } // IntPtg should be able to create itself, shouldnt have to call setValue public IntPtg(String formulaToken) { - setValue(Short.parseShort(formulaToken)); + setValue(Integer.parseInt(formulaToken)); } /** * Sets the wrapped value. * Normally you should call with a positive int. */ - public void setValue(short value) - { - field_1_value = value; - } - - /** - * Sets the unsigned value. - * (Handles conversion to the internal short value) - */ public void setValue(int value) { - if(value > Short.MAX_VALUE) { - // Need to wrap - value -= (Short.MAX_VALUE+1)*2; - } - field_1_value = (short)value; + if(value < 0 || value > (Short.MAX_VALUE + 1)*2 ) + throw new IllegalArgumentException("Unsigned short is out of range: " + value); + field_1_value = value; } /** * Returns the value as a short, which may have * been wrapped into negative numbers */ - public short getValue() + public int getValue() { return field_1_value; } @@ -102,7 +91,7 @@ public class IntPtg public void writeBytes(byte [] array, int offset) { array[ offset + 0 ] = sid; - LittleEndian.putShort(array, offset + 1, getValue()); + LittleEndian.putUShort(array, offset + 1, getValue()); } public int getSize() diff --git a/src/scratchpad/testcases/org/apache/poi/hssf/data/44297.xls b/src/scratchpad/testcases/org/apache/poi/hssf/data/44297.xls new file mode 100755 index 0000000000000000000000000000000000000000..bc65efd4e099bd236f366f2f844a395d915ce179 GIT binary patch literal 14848 zcmeHOYiv|S6#njZciRG`r7b83>#~K=2i8KP5COYYArg6|1+1~AJh}pctd?qkXjl{t zCYWF_@ehe5(H}6r}gTElNcw1Y!2$ZfSENqdgE7Eui4Dn~=q<^eTax!jFSWbDY*_2a zhJJRu1x^?ac||x#e=H`Nb(Q89yWggDzP0A+&-`4>bWcd%-a!&o805($IKr~ z-@zM}=QHEX!XzA_e0tY@b$1{da$3=YO<0R9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