From: Gordon Ball <gordon@chronitis.net>
Date: Fri, 28 May 2021 11:15:26 +0000
Subject: Fix infinite loop for monthly recurrence
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit

There is an off-by-one error when reading a PnM recurrence cycle, which
results means for n > 1, an infinite loop occurs generating the next
date.

Patch from Michał Mirosław
---
 src/recur.cpp | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/recur.cpp b/src/recur.cpp
index a18a834..6d7a8e9 100644
--- a/src/recur.cpp
+++ b/src/recur.cpp
@@ -258,7 +258,10 @@ Datetime getNextRecurrence (Datetime& current, std::string& period)
            Lexer::isAllDigits (period.substr (1, period.length () - 2)) &&
            period[period.length () - 1] == 'M')
   {
-    int increment = strtol (period.substr (0, period.length () - 1).c_str (), nullptr, 10);
+    int increment = strtol (period.substr (1, period.length () - 2).c_str (), nullptr, 10);
+
+    if (increment <= 0)
+      throw std::string (format ("The recurrence value '{1}' is not valid.", period));
 
     m += increment;
     while (m > 12)
