You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2019/02/19 06:27:04 UTC

svn commit: r1853859 - in /poi/trunk/src: java/org/apache/poi/hssf/record/common/UnicodeString.java testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java

Author: fanningpj
Date: Tue Feb 19 06:27:04 2019
New Revision: 1853859

URL: http://svn.apache.org/viewvc?rev=1853859&view=rev
Log:
[bug-63151] handle NPE in UnicodeString

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/record/common/UnicodeString.java
    poi/trunk/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/record/common/UnicodeString.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/record/common/UnicodeString.java?rev=1853859&r1=1853858&r2=1853859&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/record/common/UnicodeString.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/record/common/UnicodeString.java Tue Feb 19 06:27:04 2019
@@ -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<UnicodeString> {
-    // 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 Co
      *  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 Co
      */
     public String getDebugInfo()
     {
-        StringBuffer buffer = new StringBuffer();
+        StringBuilder buffer = new StringBuilder();
 
         buffer.append("[UNICODESTRING]\n");
         buffer.append("    .charcount       = ")

Modified: poi/trunk/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java?rev=1853859&r1=1853858&r2=1853859&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/hssf/record/common/TestUnicodeString.java Tue Feb 19 06:27:04 2019
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertEqu
 
 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.SSTRec
 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 <code>true</code> 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<numChars;i++) {
-        char ch = (char) ((i%16)+charBase);
-        b.append(ch);
-      }
-      return makeUnicodeString(b.toString());
+        StringBuilder b = new StringBuilder(numChars);
+        int charBase = is16Bit ? 0x8A00 : 'A';
+        for (int i = 0; i < numChars; i++) {
+            char ch = (char) ((i % 16) + charBase);
+            b.append(ch);
+        }
+        return makeUnicodeString(b.toString());
     }
 }



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