From fcfe01f301c52b7916f4b5e4ba570f7461e959cc Mon Sep 17 00:00:00 2001 From: Tomas Babej Date: Sun, 6 Dec 2020 20:49:35 -0500 Subject: [PATCH] recur: Prevent infinite loops with 0 periods Specifying a recurrence interval that amounts to a zero, like 'P0M', 0q or 0m causes task to fall into an infinite loop when trying to determine next recurrence dates. Detect scenarios with zero-length recurrence interval and throw an exception. Closes #2262. --- src/recur.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/recur.cpp b/src/recur.cpp index c14539c99..8a0353cc7 100644 --- a/src/recur.cpp +++ b/src/recur.cpp @@ -234,6 +234,9 @@ Datetime getNextRecurrence (Datetime& current, std::string& period) { int increment = strtol (period.substr (0, period.length () - 1).c_str (), nullptr, 10); + if (increment <= 0) + throw format ("Recurrence period '{1}' is equivalent to {2} and hence invalid.", period, increment); + m += increment; while (m > 12) { @@ -253,6 +256,9 @@ Datetime getNextRecurrence (Datetime& current, std::string& period) { int increment = strtol (period.substr (1, period.length () - 2).c_str (), nullptr, 10); + if (increment <= 0) + throw format ("Recurrence period '{1}' is equivalent to {2} and hence invalid.", period, increment); + m += increment; while (m > 12) { @@ -286,6 +292,9 @@ Datetime getNextRecurrence (Datetime& current, std::string& period) { int increment = strtol (period.substr (0, period.length () - 1).c_str (), nullptr, 10); + if (increment <= 0) + throw format ("Recurrence period '{1}' is equivalent to {2} and hence invalid.", period, increment); + m += 3 * increment; while (m > 12) {