From 01b94f181719814e1e1a1e84005cd21b61686e2e Mon Sep 17 00:00:00 2001 From: PJ Fanning Date: Wed, 28 Jul 2021 16:56:36 +0000 Subject: [PATCH] [bug-65165] invalid date values not handled correctly in DateValue function git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1891858 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/ss/formula/functions/DateValue.java | 15 ++++++++++----- .../poi/ss/formula/functions/TestDateValue.java | 16 +++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java b/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java index 57a6733f6e..79c90cc23f 100644 --- a/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java +++ b/poi/src/main/java/org/apache/poi/ss/formula/functions/DateValue.java @@ -17,14 +17,14 @@ package org.apache.poi.ss.formula.functions; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.poi.ss.formula.eval.*; import org.apache.poi.ss.util.DateParser; -import org.apache.poi.ss.formula.eval.BlankEval; -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.ss.usermodel.DateUtil; +import java.time.DateTimeException; + /** * Implementation for the DATEVALUE() Excel function.

* @@ -44,6 +44,8 @@ import org.apache.poi.ss.usermodel.DateUtil; */ public class DateValue extends Fixed1ArgFunction { + private static final Logger LOG = LogManager.getLogger(DateValue.class); + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval dateTextArg) { try { @@ -55,6 +57,9 @@ public class DateValue extends Fixed1ArgFunction { } return new NumberEval(DateUtil.getExcelDate(DateParser.parseLocalDate(dateText))); + } catch (DateTimeException dte) { + LOG.atInfo().log("Failed to parse date", dte); + return ErrorEval.VALUE_INVALID; } catch (EvaluationException e) { return e.getErrorEval(); } diff --git a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java index 9738546998..ae0ae805c3 100644 --- a/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java +++ b/poi/src/test/java/org/apache/poi/ss/formula/functions/TestDateValue.java @@ -25,11 +25,7 @@ import java.time.Year; import java.time.temporal.ChronoUnit; import java.util.Locale; -import org.apache.poi.ss.formula.eval.BlankEval; -import org.apache.poi.ss.formula.eval.ErrorEval; -import org.apache.poi.ss.formula.eval.NumberEval; -import org.apache.poi.ss.formula.eval.StringEval; -import org.apache.poi.ss.formula.eval.ValueEval; +import org.apache.poi.ss.formula.eval.*; import org.apache.poi.util.LocaleUtil; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; @@ -88,6 +84,16 @@ final class TestDateValue { } } + @Test + void testInvalidDateValue() { + assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(new StringEval("not-date")), + "not-date evals to invalid"); + assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(BoolEval.FALSE), + "false evals to invalid"); + assertEquals(ErrorEval.VALUE_INVALID, invokeDateValue(new NumberEval(Math.E)), + "Math.E evals to invalid"); + } + private ValueEval invokeDateValue(ValueEval text) { return new DateValue().evaluate(0, 0, text); } -- 2.39.5