From 603bf075f16483fef157bc713cc9a519dbce02ef Mon Sep 17 00:00:00 2001 From: "Bernhard M. Wiedemann" Date: Thu, 31 Aug 2023 04:08:31 +0200 Subject: [PATCH] Fix issues with year 2038 (#3052) * Fix annotations in year 2038 Fixes #3050 * Ensure 32-bit systems work better after 2038 Without this patch, their 32-bit signed long int could overflow. This patch was done while working on reproducible builds for openSUSE. --- src/DOM.cpp | 2 +- src/TDB2.cpp | 2 +- src/Task.cpp | 6 +++--- src/columns/ColDescription.cpp | 4 ++-- src/columns/ColUDA.cpp | 4 ++-- src/commands/CmdCalendar.cpp | 4 ++-- src/commands/CmdEdit.cpp | 2 +- src/commands/CmdInfo.cpp | 2 +- src/commands/CmdStats.cpp | 4 ++-- src/commands/CmdSummary.cpp | 6 +++--- src/feedback.cpp | 2 +- 11 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/DOM.cpp b/src/DOM.cpp index f0c5e9826..4b6f901dc 100644 --- a/src/DOM.cpp +++ b/src/DOM.cpp @@ -417,7 +417,7 @@ bool getDOM (const std::string& name, const Task* task, Variant& value) { // annotation_1234567890 // 0 ^11 - value = Variant ((time_t) strtol (i.first.substr (11).c_str (), NULL, 10), Variant::type_date); + value = Variant ((time_t) strtoll (i.first.substr (11).c_str (), NULL, 10), Variant::type_date); return true; } else if (elements[2] == "description") diff --git a/src/TDB2.cpp b/src/TDB2.cpp index 8f736db52..7a4cb7c1b 100644 --- a/src/TDB2.cpp +++ b/src/TDB2.cpp @@ -217,7 +217,7 @@ void TDB2::show_diff ( const std::string& prior, const std::string& when) { - Datetime lastChange (strtol (when.c_str (), nullptr, 10)); + Datetime lastChange (strtoll (when.c_str (), nullptr, 10)); // Set the colors. Color color_red (Context::getContext ().color () ? Context::getContext ().config.get ("color.undo.before") : ""); diff --git a/src/Task.cpp b/src/Task.cpp index c9728996e..15ffa54bc 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -199,8 +199,8 @@ const std::string Task::identifier (bool shortened /* = false */) const //////////////////////////////////////////////////////////////////////////////// void Task::setAsNow (const std::string& att) { - char now[16]; - snprintf (now, 16, "%u", (unsigned int) time (nullptr)); + char now[22]; + snprintf (now, 22, "%lli", (long long int) time (nullptr)); set (att, now); recalc_urgency = true; @@ -1189,7 +1189,7 @@ void Task::addAnnotation (const std::string& description) do { - key = "annotation_" + format ((int) now); + key = "annotation_" + format ((long long int) now); ++now; } while (has (key)); diff --git a/src/columns/ColDescription.cpp b/src/columns/ColDescription.cpp index fc77f6179..86ef52524 100644 --- a/src/columns/ColDescription.cpp +++ b/src/columns/ColDescription.cpp @@ -171,7 +171,7 @@ void ColumnDescription::render ( { for (const auto& i : task.getAnnotations ()) { - Datetime dt (strtol (i.first.substr (11).c_str (), nullptr, 10)); + Datetime dt (strtoll (i.first.substr (11).c_str (), nullptr, 10)); description += '\n' + std::string (_indent, ' ') + dt.toString (_dateformat) + ' ' + i.second; } } @@ -200,7 +200,7 @@ void ColumnDescription::render ( { for (const auto& i : task.getAnnotations ()) { - Datetime dt (strtol (i.first.substr (11).c_str (), nullptr, 10)); + Datetime dt (strtoll (i.first.substr (11).c_str (), nullptr, 10)); description += ' ' + dt.toString (_dateformat) + ' ' + i.second; } } diff --git a/src/columns/ColUDA.cpp b/src/columns/ColUDA.cpp index 7e214a099..57dd628bb 100644 --- a/src/columns/ColUDA.cpp +++ b/src/columns/ColUDA.cpp @@ -241,7 +241,7 @@ void ColumnUDADate::measure (Task& task, unsigned int& minimum, unsigned int& ma // rc.report..dateformat // rc.dateformat.report // rc.dateformat - Datetime date ((time_t) strtol (value.c_str (), nullptr, 10)); + Datetime date (strtoll (value.c_str (), nullptr, 10)); auto format = Context::getContext ().config.get ("report." + _report + ".dateformat"); if (format == "") format = Context::getContext ().config.get ("dateformat.report"); @@ -287,7 +287,7 @@ void ColumnUDADate::render ( format = Context::getContext ().config.get ("dateformat"); } - renderStringLeft (lines, width, color, Datetime ((time_t) strtol (value.c_str (), nullptr, 10)).toString (format)); + renderStringLeft (lines, width, color, Datetime (strtoll (value.c_str (), nullptr, 10)).toString (format)); } else if (_style == "indicator") { diff --git a/src/commands/CmdCalendar.cpp b/src/commands/CmdCalendar.cpp index c656cc05f..caf211ffc 100644 --- a/src/commands/CmdCalendar.cpp +++ b/src/commands/CmdCalendar.cpp @@ -631,7 +631,7 @@ std::string CmdCalendar::renderMonths ( { if(task.has("scheduled") && !coloredWithDue) { std::string scheduled = task.get ("scheduled"); - Datetime scheduleddmy (strtol (scheduled.c_str(), nullptr, 10)); + Datetime scheduleddmy (strtoll (scheduled.c_str(), nullptr, 10)); if (scheduleddmy.sameDay (date)) { @@ -640,7 +640,7 @@ std::string CmdCalendar::renderMonths ( } if(task.has("due")) { std::string due = task.get ("due"); - Datetime duedmy (strtol (due.c_str(), nullptr, 10)); + Datetime duedmy (strtoll (due.c_str(), nullptr, 10)); if (duedmy.sameDay (date)) { diff --git a/src/commands/CmdEdit.cpp b/src/commands/CmdEdit.cpp index 5c83b0eba..ca91527b0 100644 --- a/src/commands/CmdEdit.cpp +++ b/src/commands/CmdEdit.cpp @@ -261,7 +261,7 @@ std::string CmdEdit::formatTask (Task task, const std::string& dateformat) for (auto& anno : task.getAnnotations ()) { - Datetime dt (strtol (anno.first.substr (11).c_str (), nullptr, 10)); + Datetime dt (strtoll (anno.first.substr (11).c_str (), nullptr, 10)); before << " Annotation: " << dt.toString (dateformat) << " -- " << str_replace (anno.second, "\n", ANNOTATION_EDIT_MARKER) << '\n'; } diff --git a/src/commands/CmdInfo.cpp b/src/commands/CmdInfo.cpp index 30dfaa2b5..f0a1e993c 100644 --- a/src/commands/CmdInfo.cpp +++ b/src/commands/CmdInfo.cpp @@ -240,7 +240,7 @@ int CmdInfo::execute (std::string& output) auto created = task.get ("entry"); if (created.length ()) { - Datetime dt (strtol (created.c_str (), nullptr, 10)); + Datetime dt (strtoll (created.c_str (), nullptr, 10)); age = Duration (now - dt).formatVague (); } diff --git a/src/commands/CmdStats.cpp b/src/commands/CmdStats.cpp index 7e5c92089..668b1cf0d 100644 --- a/src/commands/CmdStats.cpp +++ b/src/commands/CmdStats.cpp @@ -109,13 +109,13 @@ int CmdStats::execute (std::string& output) if (task.is_blocked) ++blockedT; if (task.is_blocking) ++blockingT; - time_t entry = strtol (task.get ("entry").c_str (), nullptr, 10); + time_t entry = strtoll (task.get ("entry").c_str (), nullptr, 10); if (entry < earliest) earliest = entry; if (entry > latest) latest = entry; if (status == Task::completed) { - time_t end = strtol (task.get ("end").c_str (), nullptr, 10); + time_t end = strtoll (task.get ("end").c_str (), nullptr, 10); daysPending += (end - entry) / 86400.0; } diff --git a/src/commands/CmdSummary.cpp b/src/commands/CmdSummary.cpp index a8ddd48ac..4a23384ba 100644 --- a/src/commands/CmdSummary.cpp +++ b/src/commands/CmdSummary.cpp @@ -109,7 +109,7 @@ int CmdSummary::execute (std::string& output) { ++countPending[parent]; - time_t entry = strtol (task.get ("entry").c_str (), nullptr, 10); + time_t entry = strtoll (task.get ("entry").c_str (), nullptr, 10); if (entry) sumEntry[parent] = sumEntry[parent] + (double) (now - entry); } @@ -121,8 +121,8 @@ int CmdSummary::execute (std::string& output) { ++countCompleted[parent]; - time_t entry = strtol (task.get ("entry").c_str (), nullptr, 10); - time_t end = strtol (task.get ("end").c_str (), nullptr, 10); + time_t entry = strtoll (task.get ("entry").c_str (), nullptr, 10); + time_t end = strtoll (task.get ("end").c_str (), nullptr, 10); if (entry && end) sumEntry[parent] = sumEntry[parent] + (double) (end - entry); } diff --git a/src/feedback.cpp b/src/feedback.cpp index a7a3d54d0..ef5b6de72 100644 --- a/src/feedback.cpp +++ b/src/feedback.cpp @@ -52,7 +52,7 @@ std::string renderAttribute (const std::string& name, const std::string& value, col->type () == "date" && value != "") { - Datetime d ((time_t)strtol (value.c_str (), nullptr, 10)); + Datetime d ((time_t)strtoll (value.c_str (), nullptr, 10)); if (format == "") return d.toString (Context::getContext ().config.get ("dateformat"));