include/tools/date.hxx           |    3 +++
 sc/source/core/tool/interpr2.cxx |    9 +++++++--
 tools/source/datetime/tdate.cxx  |   11 +++++++++++
 3 files changed, 21 insertions(+), 2 deletions(-)

New commits:
commit 9b74900076e9d1e6835aa17763568f339464b0f2
Author:     Caolán McNamara <[email protected]>
AuthorDate: Thu Sep 11 11:55:38 2025 +0100
Commit:     Caolán McNamara <[email protected]>
CommitDate: Thu Sep 11 18:14:53 2025 +0200

    ofz#442383407 Integer-overflow
    
    Change-Id: I96c62fd03a6c7b4c5e1ddaa62df4bb577fbdb8ae
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190812
    Reviewed-by: Caolán McNamara <[email protected]>
    Tested-by: Jenkins

diff --git a/include/tools/date.hxx b/include/tools/date.hxx
index f2e492d8f15e..db6c1a9e7029 100644
--- a/include/tools/date.hxx
+++ b/include/tools/date.hxx
@@ -134,6 +134,9 @@ public:
      */
     void            AddDays( sal_Int32 nAddDays );
 
+    // Same as AddDays, but returns false on int overflow cases
+    [[nodiscard]] bool CheckedAddDays(sal_Int32 nAddDays);
+
     /** Obtain the day of the week for the date.
 
         Internally normalizes a copy of values.
diff --git a/sc/source/core/tool/interpr2.cxx b/sc/source/core/tool/interpr2.cxx
index 1744d3473951..8edd187df9e8 100644
--- a/sc/source/core/tool/interpr2.cxx
+++ b/sc/source/core/tool/interpr2.cxx
@@ -136,8 +136,13 @@ void ScInterpreter::ScGetMonth()
 void ScInterpreter::ScGetDay()
 {
     Date aDate = mrContext.NFGetNullDate();
-    aDate.AddDays( GetFloor32());
-    PushDouble(static_cast<double>(aDate.GetDay()));
+    if (aDate.CheckedAddDays(GetFloor32()))
+        PushDouble(static_cast<double>(aDate.GetDay()));
+    else
+    {
+        SetError(FormulaError::IllegalArgument);
+        PushDouble(HUGE_VAL);
+    }
 }
 
 void ScInterpreter::ScGetMin()
diff --git a/tools/source/datetime/tdate.cxx b/tools/source/datetime/tdate.cxx
index 3d543c8cc47e..0b2b25ffc882 100644
--- a/tools/source/datetime/tdate.cxx
+++ b/tools/source/datetime/tdate.cxx
@@ -376,6 +376,17 @@ void Date::AddDays( sal_Int32 nDays )
         *this = lcl_DaysToDate( GetAsNormalizedDays() + nDays );
 }
 
+bool Date::CheckedAddDays( sal_Int32 nDays )
+{
+    if (nDays == 0)
+        return true;
+    sal_Int32 nTotalDays;
+    if (o3tl::checked_add(GetAsNormalizedDays(), nDays, nTotalDays))
+        return false;
+    *this = lcl_DaysToDate(nTotalDays);
+    return true;
+}
+
 Date& Date::operator ++()
 {
     *this = lcl_DaysToDate( GetAsNormalizedDays() + 1 );

Reply via email to