From 6c7bf59087e720a663fc5aec7ec5b2d502d7accd Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 9 Sep 2020 07:05:09 +0000 Subject: [PATCH] [bug-64721] NullPointerException occurs when calling getDataSize() of an object created with ExtRst.copy(). Thanks to Kwon Ohyoung git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1881582 13f79535-47bb-0310-9956-ffa450edef68 --- build.gradle | 4 +- .../apache/poi/hssf/record/common/ExtRst.java | 1 + .../hssf/record/common/TestUnicodeString.java | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) diff --git a/build.gradle b/build.gradle index b8628215e3..fe6b24eff2 100644 --- a/build.gradle +++ b/build.gradle @@ -127,7 +127,7 @@ subprojects { exclude '**/BaseTestCellUtil.class' exclude '**/TestUnfixedBugs.class' exclude '**/TestOneFile.class' - +include '**/TestUnicodeString.class' // Exclude Test Suites exclude '**/All*Tests.class' exclude '**/HSSFTests.class' @@ -381,4 +381,4 @@ project('scratchpad') { } japicmp.baseline = "org.apache.poi:poi:${japicmpversion}@jar" -} \ No newline at end of file +} diff --git a/src/java/org/apache/poi/hssf/record/common/ExtRst.java b/src/java/org/apache/poi/hssf/record/common/ExtRst.java index 87e4887c52..50f41dedae 100644 --- a/src/java/org/apache/poi/hssf/record/common/ExtRst.java +++ b/src/java/org/apache/poi/hssf/record/common/ExtRst.java @@ -58,6 +58,7 @@ public class ExtRst implements Comparable, GenericRecord { } protected ExtRst(ExtRst other) { + this(); reserved = other.reserved; formattingFontIndex = other.formattingFontIndex; formattingOptions = other.formattingOptions; diff --git a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java index 9837459393..f1016745bb 100644 --- a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java @@ -367,6 +367,60 @@ public final class TestUnicodeString { wb.close(); } + @Test + public void copyExtRst() { + ExtRst ext = new ExtRst(); + + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals(0, ext.getFormattingOptions()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + assertEquals(10, ext.getDataSize()); // Excludes 4 byte header + + ExtRst copied = ext.copy(); + + assertEquals(0, copied.getNumberOfRuns()); + assertEquals(0, copied.getFormattingFontIndex()); + assertEquals(0, copied.getFormattingOptions()); + assertEquals("", copied.getPhoneticText()); + assertEquals(0, copied.getPhRuns().length); + assertEquals(10, copied.getDataSize()); + } + + @Test + public void copyExtRstFromData() { + byte[] data = new byte[]{ + 1, 0, 0x0C, 0, + 0, 0, 0x37, 0, + 0, 0, + 0, 0, 0, 0, + 0, 0 // Cruft at the end, as found from real files + }; + assertEquals(16, data.length); + + LittleEndianInputStream inp = new LittleEndianInputStream( + new ByteArrayInputStream(data) + ); + ExtRst ext = new ExtRst(inp, data.length); + assertEquals(0x0c, ext.getDataSize()); // Excludes 4 byte header + + assertEquals(0, ext.getNumberOfRuns()); + assertEquals(0x37, ext.getFormattingOptions()); + assertEquals(0, ext.getFormattingFontIndex()); + assertEquals("", ext.getPhoneticText()); + assertEquals(0, ext.getPhRuns().length); + + ExtRst copied = ext.copy(); + assertEquals(10, copied.getDataSize()); // Excludes 4 byte header + + assertEquals(0, copied.getNumberOfRuns()); + assertEquals(0x37, copied.getFormattingOptions()); + assertEquals(0, copied.getFormattingFontIndex()); + assertEquals("", copied.getPhoneticText()); + assertEquals(0, copied.getPhRuns().length); + } + private static UnicodeString makeUnicodeString(String s) { UnicodeString st = new UnicodeString(s); st.setOptionFlags((byte)0); -- 2.39.5