]> source.dussan.org Git - poi.git/commitdiff
sonar fixes - null dereference
authorAndreas Beeker <kiwiwings@apache.org>
Mon, 21 May 2018 23:17:57 +0000 (23:17 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Mon, 21 May 2018 23:17:57 +0000 (23:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1831992 13f79535-47bb-0310-9956-ffa450edef68

12 files changed:
src/examples/src/org/apache/poi/hssf/usermodel/examples/AddDimensionedImage.java
src/java/org/apache/poi/common/usermodel/fonts/FontGroup.java
src/java/org/apache/poi/hssf/model/InternalSheet.java
src/java/org/apache/poi/hssf/usermodel/EscherGraphics2d.java
src/java/org/apache/poi/poifs/crypt/ChunkedCipherOutputStream.java
src/java/org/apache/poi/poifs/crypt/xor/XOREncryptor.java
src/java/org/apache/poi/poifs/filesystem/ODocumentInputStream.java
src/java/org/apache/poi/poifs/filesystem/OPOIFSDocument.java
src/scratchpad/src/org/apache/poi/hpbf/model/HPBFPart.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFFreeformShape.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFObjectShape.java
src/scratchpad/src/org/apache/poi/hwpf/usermodel/HeaderStories.java

index 61629d5c445dc0e37fcecd33280297593d666940..bee33a250d131bde5272f3e4863f3fd7125dcfb9 100644 (file)
@@ -279,14 +279,19 @@ public class AddDimensionedImage {
         // The first two parameters are not used currently but could be if the
         // need arose to extend the functionality of this code by adding the
         // ability to specify that a clear 'border' be placed around the image.
-        anchor = new HSSFClientAnchor(0,
-                                      0,
-                                      colClientAnchorDetail.getInset(),
-                                      rowClientAnchorDetail.getInset(),
-                                      (short)colClientAnchorDetail.getFromIndex(),
-                                      rowClientAnchorDetail.getFromIndex(),
-                                      (short)colClientAnchorDetail.getToIndex(),
-                                      rowClientAnchorDetail.getToIndex());
+        int dx1 = 0, dy1 = 0, dx2 = 0, dy2 = 0;
+        short col1 = 0, col2 = 0, row1 = 0, row2 = 0;
+        if (colClientAnchorDetail != null) {
+            dx2 = colClientAnchorDetail.getInset();
+            col1 = (short)colClientAnchorDetail.getFromIndex();
+            col2 = (short)colClientAnchorDetail.getToIndex();
+        }
+        if (rowClientAnchorDetail != null) {
+            dy2 = rowClientAnchorDetail.getInset();
+            row1 = (short)rowClientAnchorDetail.getFromIndex();
+            row2 = (short)rowClientAnchorDetail.getToIndex();
+        }
+        anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
 
         // For now, set the anchor type to do not move or resize the
         // image as the size of the row/column is adjusted. This could easilly
index 191b4e0041640abcccc0709240703ccb6d6a6e4a..c8a1a1cc04645f2bc608e642dc6942f3fbe36fb9 100644 (file)
@@ -110,8 +110,11 @@ public enum FontGroup {
      * @param runText the text which font groups are to be analyzed
      * @return the FontGroup
      */
-    public static List<FontGroupRange> getFontGroupRanges(String runText) {
+    public static List<FontGroupRange> getFontGroupRanges(final String runText) {
         List<FontGroupRange> ttrList = new ArrayList<>();
+        if (runText == null || runText.isEmpty()) {
+            return ttrList;
+        }
         FontGroupRange ttrLast = null;
         final int rlen = (runText != null) ? runText.length() : 0;
         for(int cp, i = 0, charCount; i < rlen; i += charCount) {
index 83e1d37907f8231d2dbaa5ec16fa3db53c0558e7..4e8798e8866c6310d2c76d0e621e521301dd00df 100644 (file)
@@ -1377,7 +1377,9 @@ public final class InternalSheet {
             windowTwo.setFreezePanes(false);
             windowTwo.setFreezePanesNoSplit(false);
             SelectionRecord sel = (SelectionRecord) findFirstRecordBySid(SelectionRecord.sid);
-            sel.setPane(PaneInformation.PANE_UPPER_LEFT);
+            if (sel != null) {
+                sel.setPane(PaneInformation.PANE_UPPER_LEFT);
+            }
             return;
         }
 
@@ -1402,8 +1404,9 @@ public final class InternalSheet {
         windowTwo.setFreezePanesNoSplit(true);
 
         SelectionRecord sel = (SelectionRecord) findFirstRecordBySid(SelectionRecord.sid);
-        sel.setPane((byte)pane.getActivePane());
-
+        if (sel != null) {
+            sel.setPane((byte) pane.getActivePane());
+        }
     }
 
     /**
@@ -1437,7 +1440,9 @@ public final class InternalSheet {
         windowTwo.setFreezePanesNoSplit(false);
 
         SelectionRecord sel = (SelectionRecord) findFirstRecordBySid(SelectionRecord.sid);
-        sel.setPane(PANE_LOWER_RIGHT);
+        if (sel != null) {
+            sel.setPane(PANE_LOWER_RIGHT);
+        }
 
     }
 
index af3eb3463a51c33a43cb0ae5e0cfda6bc213161e..5235a70221f6b7885ebe00c30f922d80d178bd34 100644 (file)
@@ -379,7 +379,8 @@ public final class EscherGraphics2d extends Graphics2D {
     public Rectangle getClipBounds()
     {
         if(getDeviceclip() != null) {
-            return getClip().getBounds();
+            final Shape clip = getClip();
+            return clip != null ? clip.getBounds() : null;
         }
         return null;
     }
index 62835109eb03504d260b90ab0a1f1933d9fd22aa..844b5d226664937d571be9df57d11b4ec8991399 100644 (file)
@@ -218,9 +218,13 @@ public abstract class ChunkedCipherOutputStream extends FilterOutputStream {
         int ciLen = (doFinal)
             ? cipher.doFinal(chunk, 0, posInChunk, chunk)
             : cipher.update(chunk, 0, posInChunk, chunk);
-        
-        for (int i = plainByteFlags.nextSetBit(0); i >= 0 && i < posInChunk; i = plainByteFlags.nextSetBit(i+1)) {
-            chunk[i] = plain[i];
+
+        if (plain != null) {
+            int i = plainByteFlags.nextSetBit(0);
+            while (i >= 0 && i < posInChunk) {
+                chunk[i] = plain[i];
+                i = plainByteFlags.nextSetBit(i+1);
+            }
         }
         
         return ciLen;
index c090fe3610e492701b2004e30f0c68218a8dd467..61f06f26329eec08b4466ea6e4f47cb49d6b303e 100644 (file)
@@ -164,8 +164,12 @@ public class XOREncryptor extends Encryptor implements Cloneable {
                 chunk[i] = value;
             }
 
-            for (int i = plainBytes.nextSetBit(start); i >= 0 && i < posInChunk; i = plainBytes.nextSetBit(i+1)) {
-                chunk[i] = plain[i];
+            if (plain != null) {
+                int i = plainBytes.nextSetBit(start);
+                while (i >= 0 && i < posInChunk) {
+                    chunk[i] = plain[i];
+                    i = plainBytes.nextSetBit(i + 1);
+                }
             }
 
             return posInChunk;
index 97225805a4b3b5e3384189589a3b81037d357eb1..944e3f322fbd8e95a68609291419181cbfe9e4ee 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.poifs.filesystem;
 
 import java.io.IOException;
+import java.util.function.Function;
 
 import org.apache.poi.poifs.storage.DataInputBlock;
 import org.apache.poi.util.RecordFormatException;
@@ -214,40 +215,57 @@ public final class ODocumentInputStream extends DocumentInputStream {
    @Override
        public void readFully(byte[] buf, int off, int len) {
                checkAvaliable(len);
-               int blockAvailable = _currentBlock.available();
+
+               Function<Integer,DataInputBlock> nextDataInputBlock = (offset) -> {
+                       if (offset >= _document_size) {
+                               _currentBlock = null;
+                       } else if (offset != _current_offset) {
+                               _currentBlock = getDataInputBlock(offset);
+                       }
+                       return _currentBlock;
+               };
+
+               _current_offset = readFullyInternal(buf, off, len, _current_offset, _document_size, nextDataInputBlock);
+       }
+
+       /* package */ static int readFullyInternal(byte[] buf, int off, int len, int currentOffset, int maxSize, Function<Integer,DataInputBlock> nextDataInputBlock) {
+               DataInputBlock currentBlock = nextDataInputBlock.apply(currentOffset);
+               if (currentBlock == null) {
+                       throw new IllegalStateException("reached end of document stream unexpectedly");
+               }
+               int blockAvailable = currentBlock.available();
                if (blockAvailable > len) {
-                       _currentBlock.readFully(buf, off, len);
-                       _current_offset += len;
-                       return;
+                       currentBlock.readFully(buf, off, len);
+                       return currentOffset + len;
                }
                // else read big amount in chunks
                int remaining = len;
                int writePos = off;
+               int offset = currentOffset;
                while (remaining > 0) {
-                       boolean blockIsExpiring = remaining >= blockAvailable;
-                       int reqSize;
-                       if (blockIsExpiring) {
-                               reqSize = blockAvailable;
-                       } else {
-                               reqSize = remaining;
-                       }
-                       _currentBlock.readFully(buf, writePos, reqSize);
+                       final boolean blockIsExpiring = remaining >= blockAvailable;
+                       final int reqSize = (blockIsExpiring) ? blockAvailable : remaining;
+                       currentBlock.readFully(buf, writePos, reqSize);
                        remaining -= reqSize;
                        writePos += reqSize;
-                       _current_offset += reqSize;
+                       offset += reqSize;
                        if (blockIsExpiring) {
-                               if (_current_offset == _document_size) {
+                               if (offset >= maxSize) {
                                        if (remaining > 0) {
                                                throw new IllegalStateException(
                                                                "reached end of document stream unexpectedly");
                                        }
-                                       _currentBlock = null;
                                        break;
                                }
-                               _currentBlock = getDataInputBlock(_current_offset);
-                               blockAvailable = _currentBlock.available();
+                               currentBlock = nextDataInputBlock.apply(offset);
+                               if (currentBlock == null) {
+                                       throw new IllegalStateException(
+                                                       "reached end of document stream unexpectedly");
+                               }
+                               blockAvailable = currentBlock.available();
                        }
                }
+               return offset;
        }
 
    @Override
index bffc74454fb6f68555dfc2a98022008874dc2f75..43820ab532401ffa0b1c761756be0cf312967610 100644 (file)
@@ -25,6 +25,7 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
 import java.util.List;
+import java.util.function.Function;
 
 import org.apache.poi.poifs.common.POIFSBigBlockSize;
 import org.apache.poi.poifs.common.POIFSConstants;
@@ -228,43 +229,7 @@ public final class OPOIFSDocument implements BATManaged, BlockWritable, POIFSVie
         * This method is currently (Oct 2008) only used by test code. Perhaps it can be deleted
         */
        void read(byte[] buffer, int offset) {
-               int len = buffer.length;
-
-               DataInputBlock currentBlock = getDataInputBlock(offset);
-
-               int blockAvailable = currentBlock.available();
-               if (blockAvailable > len) {
-                       currentBlock.readFully(buffer, 0, len);
-                       return;
-               }
-               // else read big amount in chunks
-               int remaining = len;
-               int writePos = 0;
-               int currentOffset = offset;
-               while (remaining > 0) {
-                       boolean blockIsExpiring = remaining >= blockAvailable;
-                       int reqSize;
-                       if (blockIsExpiring) {
-                               reqSize = blockAvailable;
-                       } else {
-                               reqSize = remaining;
-                       }
-                       currentBlock.readFully(buffer, writePos, reqSize);
-                       remaining-=reqSize;
-                       writePos+=reqSize;
-                       currentOffset += reqSize;
-                       if (blockIsExpiring) {
-                               if (currentOffset == _size) {
-                                       if (remaining > 0) {
-                                               throw new IllegalStateException("reached end of document stream unexpectedly");
-                                       }
-                                       currentBlock = null;
-                                       break;
-                               }
-                               currentBlock = getDataInputBlock(currentOffset);
-                               blockAvailable = currentBlock.available();
-                       }
-               }
+               ODocumentInputStream.readFullyInternal(buffer, 0, buffer.length, offset, _size, this::getDataInputBlock);
        }
 
        /**
index 2cbc6bcd0e590c6f498ca2fc6e1edd453ff57597..49e2ac43a25ed4f589bd0a83be1502e4df5a5195 100644 (file)
@@ -64,6 +64,9 @@ public abstract class HPBFPart {
 
        public void writeOut(DirectoryNode baseDir) throws IOException {
                String[] path = getPath();
+               if (path == null) {
+                       return;
+               }
 
                // Ensure that all parent directories exist
                DirectoryNode dir = baseDir;
index 63f30bd45b9fa3188202ba9783e0676c7ccaa1e3..0b2d322b6319fa0b8f2d0248bdd08081fa72aad0 100644 (file)
@@ -301,44 +301,46 @@ public final class HSLFFreeformShape extends HSLFAutoShape implements FreeformSh
         while (vertIter.hasNext() && segIter.hasNext()) {
             byte[] segElem = segIter.next();
             PathInfo pi = getPathInfo(segElem);
-            switch (pi) {
-                case escape: {
-                    // handleEscapeInfo(path, segElem, vertIter);
-                    break;
-                }
-                case moveTo: {
-                    fillPoint(vertIter.next(), xyPoints);
-                    double x = xyPoints[0];
-                    double y = xyPoints[1];
-                    path.moveTo(x,y);
-                    break;
-                }
-                case curveTo: {
-                    fillPoint(vertIter.next(), xyPoints);
-                    double x1 = xyPoints[0];
-                    double y1 = xyPoints[1];
-                    fillPoint(vertIter.next(), xyPoints);
-                    double x2 = xyPoints[0];
-                    double y2 = xyPoints[1];
-                    fillPoint(vertIter.next(), xyPoints);
-                    double x3 = xyPoints[0];
-                    double y3 = xyPoints[1];
-                    path.curveTo(x1,y1,x2,y2,x3,y3);
-                    break;
-                }
-                case lineTo:
-                    if (vertIter.hasNext()) {
+            if (pi != null) {
+                switch (pi) {
+                    case escape: {
+                        // handleEscapeInfo(path, segElem, vertIter);
+                        break;
+                    }
+                    case moveTo: {
                         fillPoint(vertIter.next(), xyPoints);
                         double x = xyPoints[0];
                         double y = xyPoints[1];
-                        path.lineTo(x,y);
+                        path.moveTo(x, y);
+                        break;
                     }
-                    break;
-                case close:
-                    path.closePath();
-                    break;
-                default:
-                    break;
+                    case curveTo: {
+                        fillPoint(vertIter.next(), xyPoints);
+                        double x1 = xyPoints[0];
+                        double y1 = xyPoints[1];
+                        fillPoint(vertIter.next(), xyPoints);
+                        double x2 = xyPoints[0];
+                        double y2 = xyPoints[1];
+                        fillPoint(vertIter.next(), xyPoints);
+                        double x3 = xyPoints[0];
+                        double y3 = xyPoints[1];
+                        path.curveTo(x1, y1, x2, y2, x3, y3);
+                        break;
+                    }
+                    case lineTo:
+                        if (vertIter.hasNext()) {
+                            fillPoint(vertIter.next(), xyPoints);
+                            double x = xyPoints[0];
+                            double y = xyPoints[1];
+                            path.lineTo(x, y);
+                        }
+                        break;
+                    case close:
+                        path.closePath();
+                        break;
+                    default:
+                        break;
+                }
             }
         }
 
index ffe2c51b04e08c6ee4ba077b1b31053e240998ce..8845df677ef5c478e761af1debc8967a1ac0bdea 100644 (file)
@@ -212,7 +212,10 @@ public final class HSLFObjectShape extends HSLFPictureShape implements ObjectSha
     }
 
     public void setFullName(final String fullName) {
-        getExEmbed(true).setClipboardName(fullName);
+        final ExEmbed ex = getExEmbed(true);
+        if (ex != null) {
+            ex.setClipboardName(fullName);
+        }
     }
 
     @Override
@@ -222,7 +225,10 @@ public final class HSLFObjectShape extends HSLFPictureShape implements ObjectSha
     }
 
     public void setProgId(final String progId) {
-        getExEmbed(true).setProgId(progId);
+        final ExEmbed ex = getExEmbed(true);
+        if (ex != null) {
+            ex.setProgId(progId);
+        }
     }
 
     public OutputStream updateObjectData(final Application application, final ObjectMetaData metaData) throws IOException {
@@ -246,16 +252,18 @@ public final class HSLFObjectShape extends HSLFPictureShape implements ObjectSha
 
                     poifs.getRoot().setStorageClsid(md.getClassID());
 
-
                     int oid = getObjectID();
                     if (oid == 0) {
                         // assign new embedding
                         oid = ppt.addEmbed(poifs);
                         setObjectID(oid);
                     } else {
-                        ByteArrayOutputStream bos = new ByteArrayOutputStream(this.size()+1000);
-                        poifs.writeFilesystem(bos);
-                        getObjectData().setData(bos.toByteArray());
+                        final HSLFObjectData od = getObjectData();
+                        if (od != null) {
+                            ByteArrayOutputStream bos = new ByteArrayOutputStream(this.size()+1000);
+                            poifs.writeFilesystem(bos);
+                            od.setData(bos.toByteArray());
+                        }
                     }
 
                     setProgId(md.getProgId());
index 16ae1c40deaca02692bd6404525d52219e62b7be..b15757ae037860763df4a0fcd6b0b0f2d38c95d4 100644 (file)
@@ -208,15 +208,17 @@ public final class HeaderStories {
                // First page header is optional, only return
                //  if it's set
                if(pageNumber == 1) {
-                       if(getFirstHeader().length() > 0) {
-                               return getFirstHeader();
+            final String fh = getFirstHeader();
+                       if(fh != null && !fh.isEmpty()) {
+                               return fh;
                        }
                }
                // Even page header is optional, only return
                //  if it's set
                if(pageNumber % 2 == 0) {
-                       if(getEvenHeader().length() > 0) {
-                               return getEvenHeader();
+            final String eh = getEvenHeader();
+                       if(eh != null && !eh.isEmpty()) {
+                               return eh;
                        }
                }
                // Odd is the default
@@ -274,15 +276,17 @@ public final class HeaderStories {
                // First page footer is optional, only return
                //  if it's set
                if(pageNumber == 1) {
-                       if(getFirstFooter().length() > 0) {
-                               return getFirstFooter();
+                   final String ff = getFirstFooter();
+                       if(ff != null && !ff.isEmpty()) {
+                               return ff;
                        }
                }
                // Even page footer is optional, only return
                //  if it's set
                if(pageNumber % 2 == 0) {
-                       if(getEvenFooter().length() > 0) {
-                               return getEvenFooter();
+                   final String ef = getEvenFooter();
+                       if(ef != null && !ef.isEmpty()) {
+                               return ef;
                        }
                }
                // Odd is the default