aboutsummaryrefslogtreecommitdiffstats
path: root/poi
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2023-02-14 13:51:52 +0000
committerPJ Fanning <fanningpj@apache.org>2023-02-14 13:51:52 +0000
commitadcf7bf53301a9ba1abe7ce67c73e256a4260844 (patch)
tree0dae163b9c553f931841e0a47f05a417489263b2 /poi
parent660b90062ded807bd4bf50cb419457baa1acc595 (diff)
downloadpoi-adcf7bf53301a9ba1abe7ce67c73e256a4260844.tar.gz
poi-adcf7bf53301a9ba1abe7ce67c73e256a4260844.zip
add util code to rethrow fatal exceptions
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1907645 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
-rw-r--r--poi/src/main/java/org/apache/poi/poifs/nio/CleanerUtil.java5
-rw-r--r--poi/src/main/java/org/apache/poi/util/ExceptionUtil.java60
-rw-r--r--poi/src/main/java/org/apache/poi/util/XMLHelper.java33
3 files changed, 92 insertions, 6 deletions
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 11fe068abb..11b2c621d9 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
@@ -17,6 +17,7 @@
package org.apache.poi.poifs.nio;
+import org.apache.poi.util.ExceptionUtil;
import org.apache.poi.util.SuppressForbidden;
import java.io.IOException;
@@ -181,7 +182,9 @@ public final class CleanerUtil {
unmapper.invokeExact(buffer);
return null;
} catch (Throwable t) {
- return t;
+ if (ExceptionUtil.isFatal(t)) {
+ ExceptionUtil.rethrow(t);
+ }
}
});
if (error != null) {
diff --git a/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java b/poi/src/main/java/org/apache/poi/util/ExceptionUtil.java
new file mode 100644
index 0000000000..01ec1aced2
--- /dev/null
+++ b/poi/src/main/java/org/apache/poi/util/ExceptionUtil.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;
+
+/**
+ * Utilitity methods for dealing with exceptions/throwables
+ *
+ * @since POI 5.2.4
+ */
+public class ExceptionUtil {
+ private ExceptionUtil() {}
+
+ /**
+ * It is important never to catch all <code>Throwable</code>s. Some like
+ * {@link InterruptedException} should be rethrown. Based on
+ * <a href="https://www.scala-lang.org/api/2.13.10/scala/util/control/NonFatal$.html">scala.util.control.NonFatal</a>.
+ *
+ * @param throwable to check
+ * @return whether the <code>Throwable</code> is a fatal error
+ */
+ public static boolean isFatal(Throwable throwable) {
+ //similar to https://www.scala-lang.org/api/2.13.8/scala/util/control/NonFatal$.html
+ return (throwable instanceof VirtualMachineError
+ || throwable instanceof ThreadDeath
+ || throwable instanceof InterruptedException
+ || throwable instanceof LinkageError);
+ }
+
+ /**
+ * Designed to be used in conjunction with {@link #isFatal(Throwable)}.
+ * This method should be used with care.
+ *
+ * @param throwable to check
+ * @throws Throwable the input throwable if it is an <code>Error</code> or <code>RuntimeException</code>.
+ * Otherwise wraps the throwable in a RuntimeException.
+ */
+ public static void rethrow(Throwable throwable) {
+ if (throwable instanceof Error) {
+ throw (Error) throwable;
+ }
+ if (throwable instanceof RuntimeException) {
+ throw (RuntimeException) throwable;
+ }
+ throw new RuntimeException(throwable);
+ }
+}
diff --git a/poi/src/main/java/org/apache/poi/util/XMLHelper.java b/poi/src/main/java/org/apache/poi/util/XMLHelper.java
index 7ad8e3f0b0..02eecb331b 100644
--- a/poi/src/main/java/org/apache/poi/util/XMLHelper.java
+++ b/poi/src/main/java/org/apache/poi/util/XMLHelper.java
@@ -163,9 +163,15 @@ public final class XMLHelper {
// this also catches NoClassDefFoundError, which may be due to a local class path issue
// This may occur if the code is run inside a web container or a restricted JVM
// See bug 61170: https://bz.apache.org/bugzilla/show_bug.cgi?id=61170
+ if (ExceptionUtil.isFatal(re)) {
+ ExceptionUtil.rethrow(re);
+ }
logThrowable(re, "Failed to create SAXParserFactory", "-");
throw re;
} catch (Exception e) {
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
logThrowable(e, "Failed to create SAXParserFactory", "-");
throw new IllegalStateException("Failed to create SAXParserFactory", e);
}
@@ -260,6 +266,9 @@ public final class XMLHelper {
} catch (ClassNotFoundException ignored) {
// continue without log, this is expected in some setups
} catch (Throwable e) { // NOSONAR - also catch things like NoClassDefError here
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
logThrowable(e, "SAX Feature unsupported", securityManagerClassName);
}
}
@@ -273,9 +282,15 @@ public final class XMLHelper {
feature.accept(name, value);
return true;
} catch (Exception e) {
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
logThrowable(e, "SAX Feature unsupported", name);
- } catch (Error ame) {
- logThrowable(ame, "Cannot set SAX feature because outdated XML parser in classpath", name);
+ } catch (Error e) {
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
+ logThrowable(e, "Cannot set SAX feature because outdated XML parser in classpath", name);
}
return false;
}
@@ -285,10 +300,16 @@ public final class XMLHelper {
property.accept(name, value);
return true;
} catch (Exception e) {
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
logThrowable(e, "SAX Feature unsupported", name);
- } catch (Error ame) {
+ } catch (Error e) {
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
// ignore all top error object - GraalVM in native mode is not coping with java.xml error message resources
- logThrowable(ame, "Cannot set SAX feature because outdated XML parser in classpath", name);
+ logThrowable(e, "Cannot set SAX feature because outdated XML parser in classpath", name);
}
return false;
}
@@ -298,7 +319,9 @@ public final class XMLHelper {
property.accept(name, value);
return true;
} catch (Exception|Error e) {
- // ok to ignore
+ if (ExceptionUtil.isFatal(e)) {
+ ExceptionUtil.rethrow(e);
+ }
}
return false;
}