From: Andreas Beeker Date: Sun, 16 Feb 2014 16:30:24 +0000 (+0000) Subject: FindBugs fix X-Git-Tag: REL_3_11_BETA1~254 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=019f40493b0803f6cfd804ac72f21416a78c3c2d;p=poi.git FindBugs fix - fixed/checked various null pointer related entries - see http://findbugs.sourceforge.net/bugDescriptions.html#NP_NULL_PARAM_DEREF - ... NP_NULL_ON_SOME_PATH, NP_NULL_ON_SOME_PATH_EXCEPTION git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1568789 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/FontDetails.java b/src/java/org/apache/poi/hssf/usermodel/FontDetails.java index d9f42496d9..29bcf5ac4b 100644 --- a/src/java/org/apache/poi/hssf/usermodel/FontDetails.java +++ b/src/java/org/apache/poi/hssf/usermodel/FontDetails.java @@ -66,10 +66,10 @@ public class FontDetails public int getCharWidth( char c ) { Integer widthInteger = charWidths.get(Character.valueOf(c)); - if (widthInteger == null && c != 'W') { - return getCharWidth('W'); + if (widthInteger == null) { + return 'W' == c ? 0 : getCharWidth('W'); } - return widthInteger.intValue(); + return widthInteger; } public void addChars( char[] characters, int[] widths ) diff --git a/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java b/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java index 41ec97e0f2..6130311d60 100644 --- a/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java +++ b/src/java/org/apache/poi/ss/formula/functions/BaseNumberUtils.java @@ -25,7 +25,7 @@ public class BaseNumberUtils { public static double convertToDecimal(String value, int base, int maxNumberOfPlaces) throws IllegalArgumentException { - if (value != null && value.length() == 0) { + if (value == null || value.length() == 0) { return 0.0; } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/signature/DigitalCertificatePart.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/signature/DigitalCertificatePart.java index 797f12f02a..fffa768fb6 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/signature/DigitalCertificatePart.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/signature/DigitalCertificatePart.java @@ -36,7 +36,7 @@ public final class DigitalCertificatePart extends PackagePart { public DigitalCertificatePart() throws InvalidFormatException{ super(null, null, new ContentType("")); - // Review constructor + // TODO: Review constructor } @Override diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/signature/PackageDigitalSignature.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/signature/PackageDigitalSignature.java index 370e52e512..ee9bb64757 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/signature/PackageDigitalSignature.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/signature/PackageDigitalSignature.java @@ -30,6 +30,7 @@ public final class PackageDigitalSignature extends PackagePart { public PackageDigitalSignature() throws InvalidFormatException { super(null, null, new ContentType("")); + // TODO: Review constructor } @Override diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index 6450d92018..5018c71ea3 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -71,7 +71,21 @@ import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; import org.apache.xmlbeans.XmlOptions; import org.openxmlformats.schemas.officeDocument.x2006.relationships.STRelationshipId; -import org.openxmlformats.schemas.spreadsheetml.x2006.main.*; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookView; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBookViews; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCalcPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedName; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDefinedNames; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDialogsheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheets; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbook; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookPr; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorkbookProtection; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCalcMode; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSheetState; +import org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument; /** * High level representation of a SpreadsheetML workbook. This is the first object most users @@ -1240,6 +1254,8 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable 31) sheetname = sheetname.substring(0, 31); WorkbookUtil.validateSheetName(sheetname); + // findbugs fix - validateSheetName has already checked for null value + assert(sheetname != null); if (containsSheet(sheetname, sheetIndex )) throw new IllegalArgumentException( "The workbook already contains a sheet of this name" ); diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java index aeb5491175..38bf2095e7 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/ColumnHelper.java @@ -55,11 +55,13 @@ public class ColumnHelper { CTCols aggregateCols = CTCols.Factory.newInstance(); List colsList = worksheet.getColsList(); - if (colsList != null) { - for (CTCols cols : colsList) { - for (CTCol col : cols.getColList()) { - cloneCol(aggregateCols, col); - } + if (colsList == null || colsList.isEmpty()) { + return; + } + + for (CTCols cols : colsList) { + for (CTCol col : cols.getColList()) { + cloneCol(aggregateCols, col); } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 406c670c0f..978e69d351 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -65,11 +65,11 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTc; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CommentsDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.EndnotesDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.FootnotesDocument; +import org.openxmlformats.schemas.wordprocessingml.x2006.main.NumberingDocument; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect; import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument; @@ -1156,7 +1156,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { throw new POIXMLException(e); } finally { try { - out.close(); + if (out != null) out.close(); } catch (IOException e) { // ignore } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java index fa65abb2fd..5fc2fdcd07 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFHeaderFooter.java @@ -257,7 +257,7 @@ public abstract class XWPFHeaderFooter extends POIXMLDocumentPart implements IBo throw new POIXMLException(e); } finally { try { - out.close(); + if (out != null) out.close(); } catch (IOException e) { // ignore } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java index 99bd5d1943..526eea7fbe 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFParagraph.java @@ -41,7 +41,6 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRunTrackChange; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtContentRun; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtRun; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSmartTagRun; @@ -472,8 +471,11 @@ public class XWPFParagraph implements IBodyElement, IRunBody, ISDTContents { */ public void setBorderTop(Borders border) { CTPBdr ct = getCTPBrd(true); + if (ct == null) { + throw new RuntimeException("invalid paragraph state"); + } - CTBorder pr = (ct != null && ct.isSetTop()) ? ct.getTop() : ct.addNewTop(); + CTBorder pr = (ct.isSetTop()) ? ct.getTop() : ct.addNewTop(); if (border.getValue() == Borders.NONE.getValue()) ct.unsetTop(); else diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java index e819da5dbb..d7f2253586 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFPictureData.java @@ -160,7 +160,7 @@ public class XWPFPictureData extends POIXMLDocumentPart { throw new POIXMLException(e); } finally { try { - is.close(); + if (is != null) is.close(); } catch (IOException e) { throw new POIXMLException(e); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java index bac458087a..a02d82ce06 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/RichTextRun.java @@ -29,8 +29,8 @@ import org.apache.poi.hslf.model.textproperties.ParagraphFlagsTextProp; import org.apache.poi.hslf.model.textproperties.TextProp; import org.apache.poi.hslf.model.textproperties.TextPropCollection; import org.apache.poi.hslf.record.ColorSchemeAtom; -import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; /** @@ -325,6 +325,7 @@ public final class RichTextRun { // paragraphStyle will now be defined } + assert(paragraphStyle!=null); TextProp tp = fetchOrAddTextProp(paragraphStyle, propName); tp.setValue(val); } @@ -340,6 +341,7 @@ public final class RichTextRun { // characterStyle will now be defined } + assert(characterStyle!=null); TextProp tp = fetchOrAddTextProp(characterStyle, propName); tp.setValue(val); } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java index 70d0c59553..81ff0c91f6 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java @@ -97,7 +97,7 @@ public final class SprmUtils break; default: //should never happen - break; + throw new RuntimeException("Invalid sprm type"); } LittleEndian.putShort(sprm, 0, instruction); list.add(sprm); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/SectionProperties.java b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/SectionProperties.java index 8205e6be5d..031cdb0e71 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/usermodel/SectionProperties.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/usermodel/SectionProperties.java @@ -62,7 +62,7 @@ public final class SectionProperties extends SEPAbstractType { continue; } - if ( !obj1.equals( obj2 ) ) + if ( obj1 == null || obj2 == null || !obj1.equals( obj2 ) ) { return false; }