From 3ef2cd70a445cc34c0e4e24ee535739b9dd5fd80 Mon Sep 17 00:00:00 2001 From: Pete Nelson Date: Sat, 14 Mar 2015 16:56:35 -0400 Subject: [PATCH] Add defensive check for localtime_r() call --- src/util.cc | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/util.cc b/src/util.cc index ac2a942ed3..0ae397e5b7 100644 --- a/src/util.cc +++ b/src/util.cc @@ -1356,9 +1356,13 @@ double calc_next_rotate(double current, double interval, double base) time_t teatime = time_t(current); struct tm t; - t = *localtime_r(&teatime, &t); - t.tm_hour = t.tm_min = t.tm_sec = 0; - double startofday = mktime(&t); + if ( ! localtime_r(&teatime, &t) ) + { + reporter->Error("calc_next_rotate(): can't parse current time"); + + // fall back to the method used if no base time is given + base = -1; + } if ( base < 0 ) // No base time given. To get nice timestamps, we round @@ -1367,6 +1371,8 @@ double calc_next_rotate(double current, double interval, double base) + interval - current; // current < startofday + base + i * interval <= current + interval + t.tm_hour = t.tm_min = t.tm_sec = 0; + double startofday = mktime(&t); return startofday + base + ceil((current - startofday - base) / interval) * interval - current;