From bf4396b37591f9a4172a3a247ea6f8634ac82681 Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Tue, 19 Feb 2019 06:27:04 +0000 Subject: [PATCH] [bug-63151] handle NPE in UnicodeString git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1853859 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/common/UnicodeString.java | 13 ++--- .../hssf/record/common/TestUnicodeString.java | 47 +++++++++++++++---- 2 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java index e20b5d5211..ef4016e598 100644 --- a/src/java/org/apache/poi/hssf/record/common/UnicodeString.java +++ b/src/java/org/apache/poi/hssf/record/common/UnicodeString.java @@ -44,8 +44,7 @@ import org.apache.poi.util.StringUtil; * REFERENCE: PG 951 Excel Binary File Format (.xls) Structure Specification v20091214 */ public class UnicodeString implements Comparable { - // TODO - make this final when the compatibility version is removed - private static POILogger _logger = POILogFactory.getLogger(UnicodeString.class); + private static final POILogger _logger = POILogFactory.getLogger(UnicodeString.class); //arbitrarily selected; may need to increase private static final int MAX_RECORD_LENGTH = 100_000; @@ -673,9 +672,11 @@ public class UnicodeString implements Comparable { * removed / re-ordered */ public void swapFontUse(short oldFontIndex, short newFontIndex) { - for (FormatRun run : field_4_format_runs) { - if(run._fontIndex == oldFontIndex) { - run._fontIndex = newFontIndex; + if (field_4_format_runs != null) { + for (FormatRun run : field_4_format_runs) { + if(run._fontIndex == oldFontIndex) { + run._fontIndex = newFontIndex; + } } } } @@ -700,7 +701,7 @@ public class UnicodeString implements Comparable { */ public String getDebugInfo() { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append("[UNICODESTRING]\n"); buffer.append(" .charcount = ") 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 b50d529844..e9373049d4 100644 --- a/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java +++ b/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java @@ -21,6 +21,7 @@ import static org.junit.Assert.assertEquals; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.IOException; import org.apache.poi.hssf.record.ContinueRecord; import org.apache.poi.hssf.record.RecordInputStream; @@ -28,6 +29,12 @@ import org.apache.poi.hssf.record.SSTRecord; import org.apache.poi.hssf.record.common.UnicodeString.ExtRst; import org.apache.poi.hssf.record.common.UnicodeString.FormatRun; import org.apache.poi.hssf.record.cont.ContinuableRecordOutput; +import org.apache.poi.hssf.usermodel.HSSFOptimiser; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.util.LittleEndianByteArrayInputStream; import org.apache.poi.util.LittleEndianByteArrayOutputStream; import org.apache.poi.util.LittleEndianConsts; @@ -344,26 +351,46 @@ public final class TestUnicodeString { assertEquals(extRst1.hashCode(), extRst2.hashCode()); } + @Test + public void unicodeStringsNullPointer() throws IOException { + HSSFWorkbook wb = new HSSFWorkbook(); + + Sheet sheet = wb.createSheet("styles"); + Row row = sheet.createRow(0); + Cell cell = row.createCell(0); + + CellStyle style = wb.createCellStyle(); + style.setFont(wb.createFont()); + cell.setCellStyle(style); + + cell.setCellValue("test"); + + HSSFOptimiser.optimiseFonts(wb); + + wb.close(); + } + private static UnicodeString makeUnicodeString(String s) { - UnicodeString st = new UnicodeString(s); - st.setOptionFlags((byte)0); - return st; + UnicodeString st = new UnicodeString(s); + st.setOptionFlags((byte)0); + return st; } private static UnicodeString makeUnicodeString(int numChars) { return makeUnicodeString(numChars, false); } + /** * @param is16Bit if true the created string will have characters > 0x00FF * @return a string of the specified number of characters */ private static UnicodeString makeUnicodeString(int numChars, boolean is16Bit) { - StringBuffer b = new StringBuffer(numChars); - int charBase = is16Bit ? 0x8A00 : 'A'; - for (int i=0;i