From 28811d26dfd4aab9560683ace083f61ce1834b63 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sat, 30 Dec 2023 11:11:36 +0000 Subject: [PATCH] Bug 66425: Avoid exceptions found via poi-fuzz Prevent ClassCastException Fixes https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=63375 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1914990 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/poi/ss/usermodel/FractionFormat.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/FractionFormat.java b/poi/src/main/java/org/apache/poi/ss/usermodel/FractionFormat.java index 75c343b33f..9c0b081c19 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/FractionFormat.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/FractionFormat.java @@ -44,7 +44,7 @@ import org.apache.poi.ss.formula.eval.NotImplementedException; @SuppressWarnings("serial") public class FractionFormat extends Format { private static final Logger LOGGER = LogManager.getLogger(FractionFormat.class); - private static final Pattern DENOM_FORMAT_PATTERN = Pattern.compile("(?:(#+)|(\\d+))"); + private static final Pattern DENOM_FORMAT_PATTERN = Pattern.compile("(#+)|(\\d+)"); //this was chosen to match the earlier limitation of max denom power //it can be expanded to get closer to Excel's calculations @@ -189,6 +189,10 @@ public class FractionFormat extends Format { @Override public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { + if (!(obj instanceof Number)) { + throw new IllegalArgumentException("Cannot format object of " + obj.getClass() + " to number: " + obj); + } + return toAppendTo.append(format((Number)obj)); } -- 2.39.5