Skip to content

Commit

Permalink
Fix issues with year 2038 (#3052)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
bmwiedemann committed Aug 31, 2023
1 parent 7017d8f commit 603bf07
Show file tree
Hide file tree
Showing 11 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/DOM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/TDB2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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") : "");
Expand Down
6 changes: 3 additions & 3 deletions src/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/columns/ColDescription.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/columns/ColUDA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ void ColumnUDADate::measure (Task& task, unsigned int& minimum, unsigned int& ma
// rc.report.<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");
Expand Down Expand Up @@ -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")
{
Expand Down
4 changes: 2 additions & 2 deletions src/commands/CmdCalendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand All @@ -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))
{
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CmdEdit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/CmdInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/CmdStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/CmdSummary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/feedback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down

0 comments on commit 603bf07

Please sign in to comment.