diff options
author | PJ Fanning <fanningpj@apache.org> | 2023-02-14 14:26:56 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2023-02-14 14:26:56 +0000 |
commit | 3a70d5e0023c7e9a9f2d6d118c78ad6b345f3bd3 (patch) | |
tree | 932aac1ebddaec38c4cdc9c3dd6c2d8777b095fd /poi | |
parent | 03f7f911b0e54ba106cfc7c7fa6044a3e53d3034 (diff) | |
download | poi-3a70d5e0023c7e9a9f2d6d118c78ad6b345f3bd3.tar.gz poi-3a70d5e0023c7e9a9f2d6d118c78ad6b345f3bd3.zip |
fix up use of ExceptionUtil in main poi module
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907651 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
34 files changed, 224 insertions, 2 deletions
diff --git a/poi/src/main/java/org/apache/poi/POIDocument.java b/poi/src/main/java/org/apache/poi/POIDocument.java index 608c14be6d..de49c4fbdc 100644 --- a/poi/src/main/java/org/apache/poi/POIDocument.java +++ b/poi/src/main/java/org/apache/poi/POIDocument.java @@ -45,6 +45,7 @@ import org.apache.poi.poifs.crypt.cryptoapi.CryptoAPIEncryptor; import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.POIFSFileSystem; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; @@ -245,6 +246,9 @@ public abstract class POIDocument implements Closeable { } catch (IOException e) { throw e; } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new IOException("Error "+step+" property set with name " + setName, e); } finally { IOUtils.closeQuietly(encPoifs); diff --git a/poi/src/main/java/org/apache/poi/hpsf/Property.java b/poi/src/main/java/org/apache/poi/hpsf/Property.java index 5587042f0c..242393ef31 100644 --- a/poi/src/main/java/org/apache/poi/hpsf/Property.java +++ b/poi/src/main/java/org/apache/poi/hpsf/Property.java @@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.poi.hpsf.wellknown.PropertyIDMap; import org.apache.poi.util.CodePageUtil; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.HexDump; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianByteArrayInputStream; @@ -403,6 +404,9 @@ public class Property { try { write(bos, codepage); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOG.atWarn().withThrowable(e).log("can't serialize string"); } @@ -475,6 +479,9 @@ public class Property { return LocaleUtil.getLocaleFromLCID(((Number)value).intValue()); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOG.atWarn().log("Can't decode id {}", box(getID())); } return null; diff --git a/poi/src/main/java/org/apache/poi/hpsf/Section.java b/poi/src/main/java/org/apache/poi/hpsf/Section.java index 5ebaf43ad3..84ca0371d0 100644 --- a/poi/src/main/java/org/apache/poi/hpsf/Section.java +++ b/poi/src/main/java/org/apache/poi/hpsf/Section.java @@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.poi.hpsf.wellknown.PropertyIDMap; import org.apache.poi.util.CodePageUtil; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianByteArrayInputStream; @@ -547,6 +548,9 @@ public class Section { } catch (HPSFRuntimeException ex) { throw ex; } catch (Exception ex) { + if (ExceptionUtil.isFatal(ex)) { + ExceptionUtil.rethrow(ex); + } throw new HPSFRuntimeException(ex); } } diff --git a/poi/src/main/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java b/poi/src/main/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java index 63ed8e1fad..579b9585d1 100644 --- a/poi/src/main/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java +++ b/poi/src/main/java/org/apache/poi/hssf/record/crypto/Biff8DecryptingStream.java @@ -25,6 +25,7 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.crypt.ChunkedCipherInputStream; import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.EncryptionInfo; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; @@ -61,6 +62,9 @@ public final class Biff8DecryptingStream implements BiffHeaderInput, LittleEndia ccis.readFully(initialBuf); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new RecordFormatException(e); } } diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 12a4ff008a..fd8cca72ae 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -105,6 +105,7 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.WorkbookUtil; import org.apache.poi.util.Configurator; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.HexDump; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; @@ -1584,6 +1585,9 @@ public final class HSSFWorkbook extends POIDocument implements Workbook { } os.close(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException(e); } } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java b/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java index 28babc0c0f..d3e1007f30 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/CryptoFunctions.java @@ -34,6 +34,7 @@ import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.RC2ParameterSpec; import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.Internal; import org.apache.poi.util.LittleEndian; @@ -346,6 +347,9 @@ public final class CryptoFunctions { Class<Provider> clazz = (Class<Provider>)cl.loadClass(bcProviderName); Security.addProvider(clazz.getDeclaredConstructor().newInstance()); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException("Only the BouncyCastle provider supports your encryption settings - please add it to the classpath.", e); } } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/EncryptionInfo.java b/poi/src/main/java/org/apache/poi/poifs/crypt/EncryptionInfo.java index 30fc9539e3..701552980b 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/EncryptionInfo.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/EncryptionInfo.java @@ -35,6 +35,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LittleEndianInput; /** @@ -141,6 +142,9 @@ public class EncryptionInfo implements GenericRecord { try { eib = getBuilder(encryptionMode); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new IOException(e); } @@ -187,6 +191,9 @@ public class EncryptionInfo implements GenericRecord { try { eib = getBuilder(encryptionMode); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException(e); } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionHeader.java b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionHeader.java index 78c289f5ea..499f534695 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionHeader.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionHeader.java @@ -24,6 +24,7 @@ import org.apache.poi.poifs.crypt.ChainingMode; import org.apache.poi.poifs.crypt.CipherAlgorithm; import org.apache.poi.poifs.crypt.EncryptionHeader; import org.apache.poi.poifs.crypt.HashAlgorithm; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.GenericRecordUtil; public class AgileEncryptionHeader extends EncryptionHeader { @@ -48,6 +49,9 @@ public class AgileEncryptionHeader extends EncryptionHeader { throw new NullPointerException("keyData not set"); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException("Unable to parse keyData"); } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java b/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java index 4f229e9a18..acbff64cc0 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java @@ -42,6 +42,7 @@ import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.util.BitField; import org.apache.poi.util.BitFieldFactory; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndianInputStream; @@ -212,6 +213,8 @@ public class CryptoAPIDecryptor extends Decryptor { throw (GeneralSecurityException)e; } else if (e instanceof IOException) { throw (IOException)e; + } else if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); } else { throw new IOException("summary entries can't be read", e); } diff --git a/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java b/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java index 34c4a74675..d0bbf44887 100644 --- a/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java +++ b/poi/src/main/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDocumentOutputStream.java @@ -24,6 +24,7 @@ import javax.crypto.Cipher; import org.apache.commons.io.input.BoundedInputStream; import org.apache.commons.io.output.ByteArrayOutputStream; import org.apache.poi.EncryptedDocumentException; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.Internal; /** @@ -59,6 +60,9 @@ import org.apache.poi.util.Internal; cipher.update(oneByte, 0, 1, oneByte, 0); super.write(oneByte); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException(e); } } @@ -69,6 +73,9 @@ import org.apache.poi.util.Internal; cipher.update(b, off, len, b, off); super.write(b, off, len); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new EncryptedDocumentException(e); } } diff --git a/poi/src/main/java/org/apache/poi/poifs/nio/CleanerUtil.java b/poi/src/main/java/org/apache/poi/poifs/nio/CleanerUtil.java index 11b2c621d9..029734f8be 100644 --- a/poi/src/main/java/org/apache/poi/poifs/nio/CleanerUtil.java +++ b/poi/src/main/java/org/apache/poi/poifs/nio/CleanerUtil.java @@ -180,14 +180,17 @@ public final class CleanerUtil { (PrivilegedAction<Throwable>) () -> { try { unmapper.invokeExact(buffer); - return null; } catch (Throwable t) { if (ExceptionUtil.isFatal(t)) { ExceptionUtil.rethrow(t); } } + return null; }); if (error != null) { + if (ExceptionUtil.isFatal(error)) { + ExceptionUtil.rethrow(error); + } throw new IOException("Unable to unmap the mapped buffer", error); } }; diff --git a/poi/src/main/java/org/apache/poi/sl/draw/DrawPictureShape.java b/poi/src/main/java/org/apache/poi/sl/draw/DrawPictureShape.java index 764f1f576a..96a0b1f314 100644 --- a/poi/src/main/java/org/apache/poi/sl/draw/DrawPictureShape.java +++ b/poi/src/main/java/org/apache/poi/sl/draw/DrawPictureShape.java @@ -35,6 +35,7 @@ import org.apache.poi.poifs.filesystem.FileMagic; import org.apache.poi.sl.usermodel.PictureData; import org.apache.poi.sl.usermodel.PictureShape; import org.apache.poi.sl.usermodel.RectAlign; +import org.apache.poi.util.ExceptionUtil; public class DrawPictureShape extends DrawSimpleShape { @@ -103,7 +104,10 @@ public class DrawPictureShape extends DrawSimpleShape { } } catch (NoSuchElementException ignored) { break; - } catch (Exception | ServiceConfigurationError ignored) { + } catch (Exception | ServiceConfigurationError e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } } } diff --git a/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java b/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java index bdaccb7d6c..d6078e79bc 100644 --- a/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java +++ b/poi/src/main/java/org/apache/poi/ss/extractor/EmbeddedExtractor.java @@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.ShapeContainer; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.Beta; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.IOUtils; import org.apache.poi.util.LocaleUtil; @@ -132,6 +133,9 @@ public class EmbeddedExtractor implements Iterable<EmbeddedExtractor> { data = new EmbeddedData(od.getFileName(), od.getObjectData(), od.getContentType()); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOG.atWarn().withThrowable(e).log("Entry not found / readable - ignoring OLE embedding"); } } else if (shape instanceof Picture) { diff --git a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java index 54199cbfca..e2e0d65d1a 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/EvaluationConditionalFormatRule.java @@ -51,6 +51,7 @@ import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.StringUtil; @@ -509,6 +510,9 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon // see TextFunction.TRIM for implementation return StringUtil.isBlank(v); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } // not a valid string value, and not a blank cell (that's checked earlier) return false; } @@ -518,6 +522,9 @@ public class EvaluationConditionalFormatRule implements Comparable<EvaluationCon // see TextFunction.TRIM for implementation return StringUtil.isNotBlank(v); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } // not a valid string value, but not blank return true; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java index 37db5d6e04..82c8727752 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/OperationEvaluationContext.java @@ -42,6 +42,7 @@ import org.apache.poi.ss.formula.ptg.Ref3DPtg; import org.apache.poi.ss.formula.ptg.Ref3DPxg; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.util.CellReference.NameType; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LocaleUtil; /** @@ -257,6 +258,9 @@ public final class OperationEvaluationContext { } return new LazyAreaEval(0, absoluteC, ssVersion.getLastRowIndex(), absoluteC, sre); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.REF_INVALID; } } @@ -279,6 +283,9 @@ public final class OperationEvaluationContext { } return new LazyAreaEval(absoluteR, 0, absoluteR, ssVersion.getLastColumnIndex(), sre); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.REF_INVALID; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java b/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java index 26eca119c1..caf1e49394 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/SheetNameFormatter.java @@ -23,6 +23,7 @@ import java.util.regex.Pattern; import org.apache.poi.ss.util.CellReference; import org.apache.poi.ss.SpreadsheetVersion; +import org.apache.poi.util.ExceptionUtil; /** * Formats sheet names for use in formula expressions. @@ -68,6 +69,9 @@ public final class SheetNameFormatter { appendAndEscape(out, rawSheetName); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new IllegalStateException(e); } } @@ -96,6 +100,9 @@ public final class SheetNameFormatter { appendOrREF(out, rawSheetName); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new IllegalStateException(e); } } @@ -125,6 +132,9 @@ public final class SheetNameFormatter { sb.append(ch); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } throw new IllegalStateException(e); } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java b/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java index 2d22023426..9923bd0b47 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/WorkbookEvaluator.java @@ -40,6 +40,7 @@ import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.util.CellRangeAddressBase; import org.apache.poi.ss.util.CellReference; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.Internal; import static org.apache.logging.log4j.util.Unbox.box; @@ -332,6 +333,9 @@ public final class WorkbookEvaluator { String msg = "Error evaluating cell " + cr.formatAsString(); return new NotImplementedException(msg, inner); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } // avoid bombing out during exception handling LOG.atError().withThrowable(e).log("Can't add exception info"); return inner; // preserve original exception diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/Switch.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/Switch.java index b70eb4f748..6dac9212fd 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/Switch.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/Switch.java @@ -24,6 +24,7 @@ import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.functions.FreeRefFunction; +import org.apache.poi.util.ExceptionUtil; import static org.apache.poi.ss.formula.eval.RelationalOperationEval.EqualEval; @@ -53,6 +54,9 @@ public final class Switch implements FreeRefFunction { try { expression = OperandResolver.getSingleValue(args[0], ec.getRowIndex(), ec.getColumnIndex()); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.NA; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java index ccef07a1d7..fc474ad90e 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/XLookupFunction.java @@ -23,6 +23,7 @@ import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.formula.functions.ArrayFunction; import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.formula.functions.LookupUtils; +import org.apache.poi.util.ExceptionUtil; /** * Implementation of Excel function XLOOKUP() @@ -81,6 +82,9 @@ final class XLookupFunction implements FreeRefFunction, ArrayFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; } } @@ -93,6 +97,9 @@ final class XLookupFunction implements FreeRefFunction, ArrayFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java index f29a393f5f..6689facc52 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/atp/XMatchFunction.java @@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.TwoDEval; import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.formula.functions.FreeRefFunction; import org.apache.poi.ss.formula.functions.LookupUtils; +import org.apache.poi.util.ExceptionUtil; /** * Implementation of Excel function XMATCH() @@ -64,6 +65,9 @@ final class XMatchFunction implements FreeRefFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; } } @@ -76,6 +80,9 @@ final class XMatchFunction implements FreeRefFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Areas.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Areas.java index 1a7ed187d4..5d8ed35779 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Areas.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Areas.java @@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.RefListEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.ptg.NumberPtg; +import org.apache.poi.util.ExceptionUtil; /** * Returns the number of areas in a reference. An area is a range of contiguous cells or a single cell. @@ -42,6 +43,9 @@ public final class Areas implements Function { } return new NumberEval(new NumberPtg(result)); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Correl.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Correl.java index 0aef78249a..0fa23c6641 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Correl.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Correl.java @@ -21,6 +21,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.util.ExceptionUtil; import java.util.List; @@ -57,6 +58,9 @@ public class Correl extends Fixed2ArgFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.NA; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Covar.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Covar.java index 2aa916a2ae..8b6aaf4e57 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Covar.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Covar.java @@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.util.ExceptionUtil; import java.util.List; @@ -57,6 +58,9 @@ public class Covar extends Fixed2ArgFunction implements FreeRefFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.NA; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java index 8007ad06e0..5d4196ce16 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DStarRunner.java @@ -32,6 +32,7 @@ import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.StringValueEval; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.util.NumberComparer; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.Internal; import org.apache.poi.util.LocaleUtil; @@ -136,6 +137,9 @@ public final class DStarRunner implements Function3Arg { return e.getErrorEval(); } } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } if (!algorithm.allowEmptyMatchField()) { return ErrorEval.VALUE_INVALID; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java index 1075680657..1fc277b6ff 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Days.java @@ -28,6 +28,7 @@ import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.util.DateParser; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LocaleUtil; /** @@ -82,6 +83,9 @@ public class Days implements FreeRefFunction { double d0 = NumericFunction.singleOperandEvaluate(ve, srcRowIndex, srcColumnIndex); return getDate(d0); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } String strText1 = OperandResolver.coerceValueToString(ve); return DateParser.parseLocalDate(strText1); } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Forecast.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Forecast.java index b6b3f1c5f5..7b79e31c53 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Forecast.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Forecast.java @@ -22,6 +22,7 @@ import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.util.ExceptionUtil; import java.util.List; @@ -73,6 +74,9 @@ public class Forecast extends Fixed3ArgFunction implements FreeRefFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.NA; } } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/Indirect.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/Indirect.java index 9cf3ade073..cc049557c8 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/Indirect.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/Indirect.java @@ -31,6 +31,7 @@ import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.ptg.Area3DPxg; import org.apache.poi.ss.usermodel.Table; +import org.apache.poi.util.ExceptionUtil; /** * Implementation for Excel function INDIRECT<p> @@ -142,6 +143,9 @@ public final class Indirect implements FreeRefFunction { try { return ec.getDynamicReference(workbookName, sheetName, refStrPart1, refStrPart2, isA1style); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOGGER.atWarn().log("Indirect function: failed to parse reference {}", text, e); return ErrorEval.REF_INVALID; } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumberValueFunction.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumberValueFunction.java index eb0c0af009..fd06056292 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/NumberValueFunction.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/NumberValueFunction.java @@ -25,6 +25,7 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.EvaluationException; import org.apache.poi.ss.formula.eval.OperandResolver; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LocaleUtil; /** @@ -111,6 +112,9 @@ public final class NumberValueFunction implements FreeRefFunction { } catch (EvaluationException e) { return e.getErrorEval(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.VALUE_INVALID; //If any of the arguments are not valid, NUMBERVALUE returns the #VALUE! error value. } diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/TimeValue.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/TimeValue.java index 2bb56e36be..20109d1ff1 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/TimeValue.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/TimeValue.java @@ -22,6 +22,7 @@ import org.apache.logging.log4j.Logger; import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.util.DateParser; +import org.apache.poi.util.ExceptionUtil; import java.time.DateTimeException; import java.time.LocalDate; @@ -60,11 +61,17 @@ public class TimeValue extends Fixed1ArgFunction { try { return parseTimeFromDateTime(dateTimeText); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } try { //this could be a time (with no date part) - prepend a dummy date because //parseTimeFromDateTime needs it return parseTimeFromDateTime("1/01/2000 " + dateTimeText); } catch (Exception e2) { + if (ExceptionUtil.isFatal(e2)) { + ExceptionUtil.rethrow(e2); + } LocalDate ld = DateParser.parseLocalDate(dateTimeText); //return 0 as this is a pure date with no time element return new NumberEval(0); diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java index c9101dc71a..96a71d464b 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/WeekNum.java @@ -33,6 +33,7 @@ import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.util.ExceptionUtil; /** * Implementation for Excel WeekNum() function. @@ -69,6 +70,9 @@ public class WeekNum extends Fixed2ArgFunction implements FreeRefFunction { .atZone(ZoneId.systemDefault()) .toLocalDate(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return ErrorEval.NUM_ERROR; } int returnType; diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java index fe14527066..4afbfbfd75 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/DataFormatter.java @@ -47,6 +47,7 @@ import org.apache.poi.ss.format.CellFormatResult; import org.apache.poi.ss.formula.ConditionalFormattingEvaluator; import org.apache.poi.ss.util.DateFormatConverter; import org.apache.poi.ss.util.NumberToTextConverter; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.StringUtil; @@ -407,6 +408,9 @@ public class DataFormatter { // Wrap and return (non-cacheable - CellFormat does that) return new CellFormatResultWrapper( cfmt.apply(cellValueO) ); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOG.atWarn().withThrowable(e).log("Formatting failed for format {}, falling back", formatStr); } } @@ -1119,6 +1123,9 @@ public class DataFormatter { try { cellType = cell.getCachedFormulaResultType(); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } return cell.getCellFormula(); } } else { diff --git a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java index 130169999c..9d227464dc 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/SheetUtil.java @@ -38,6 +38,7 @@ import org.apache.poi.ss.usermodel.RichTextString; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.ExceptionUtil; import org.apache.poi.util.Internal; @@ -192,6 +193,9 @@ public class SheetUtil { try { sval = formatter.formatCellValue(cell, dummyEvaluator); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } sval = String.valueOf(cell.getNumericCellValue()); } } else if (cellType == CellType.BOOLEAN) { diff --git a/poi/src/main/java/org/apache/poi/util/Configurator.java b/poi/src/main/java/org/apache/poi/util/Configurator.java index b82ad9b8c7..bc876c0d37 100644 --- a/poi/src/main/java/org/apache/poi/util/Configurator.java +++ b/poi/src/main/java/org/apache/poi/util/Configurator.java @@ -34,6 +34,9 @@ public class Configurator { try { return Integer.parseInt(property); } catch (Exception e) { + if (ExceptionUtil.isFatal(e)) { + ExceptionUtil.rethrow(e); + } LOG.atError().log("System property -D{} does not contains a valid integer: {}", systemProperty, property); return defaultValue; } diff --git a/poi/src/test/java/org/apache/poi/util/ExceptionUtilTest.java b/poi/src/test/java/org/apache/poi/util/ExceptionUtilTest.java new file mode 100644 index 0000000000..d5d4fdd77a --- /dev/null +++ b/poi/src/test/java/org/apache/poi/util/ExceptionUtilTest.java @@ -0,0 +1,60 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.util; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class ExceptionUtilTest { + @Test + void testRuntimeException() { + assertFalse(ExceptionUtil.isFatal(new RuntimeException("runtime issue"))); + } + + @Test + void testPlainException() { + assertFalse(ExceptionUtil.isFatal(new Exception("plain issue"))); + } + + @Test + void testOutOfMemoryError() { + assertTrue(ExceptionUtil.isFatal(new OutOfMemoryError("oom"))); + } + + @Test + void testVirtualMachineError() { + assertTrue(ExceptionUtil.isFatal(new VirtualMachineError(){})); + } + + + @Test + void testThreadDeath() { + assertTrue(ExceptionUtil.isFatal(new ThreadDeath())); + } + + @Test + void testInterruptedException() { + assertTrue(ExceptionUtil.isFatal(new InterruptedException())); + } + + @Test + void testLinkageError() { + assertTrue(ExceptionUtil.isFatal(new LinkageError())); + } +} |