Navigation Menu

Skip to content

Commit

Permalink
Add overflow/underflow checks to find errors
Browse files Browse the repository at this point in the history
Redmine: ref #2580
  • Loading branch information
s-yata committed Jun 20, 2014
1 parent 5ec7175 commit 1404bf0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
35 changes: 22 additions & 13 deletions lib/mrn_time_converter.cpp
Expand Up @@ -22,6 +22,8 @@
# include <config.h>
#endif

#include <limits>

#include "mrn_time_converter.hpp"

// for debug
Expand All @@ -34,15 +36,19 @@ namespace mrn {
TimeConverter::~TimeConverter() {
}

time_t TimeConverter::tm_to_time_gm(struct tm *time) {
time_t TimeConverter::tm_to_time_gm(struct tm *time, bool *truncated) {
MRN_DBUG_ENTER_METHOD();
*truncated = true;
struct tm gmdate;
time->tm_yday = -1;
time->tm_isdst = -1;
time_t sec_t = mktime(time);
if (time->tm_yday == -1)
DBUG_RETURN(sec_t);
gmtime_r(&sec_t, &gmdate);
if (time->tm_yday == -1) {
DBUG_RETURN(-1);
}
if (!gmtime_r(&sec_t, &gmdate)) {
DBUG_RETURN(-1);
}
int32 mrn_utc_diff_in_seconds =
(
time->tm_mday > 25 && gmdate.tm_mday == 1 ? -1 :
Expand All @@ -60,26 +66,29 @@ namespace mrn {
DBUG_PRINT("info", ("mroonga: time->tm_sec=%d", time->tm_sec));
DBUG_PRINT("info", ("mroonga: mrn_utc_diff_in_seconds=%d",
mrn_utc_diff_in_seconds));
if (mrn_utc_diff_in_seconds > 0) {
if (sec_t > std::numeric_limits<time_t>::max() - mrn_utc_diff_in_seconds) {
DBUG_RETURN(-1);
}
} else {
if (sec_t < std::numeric_limits<time_t>::min() - mrn_utc_diff_in_seconds) {
DBUG_RETURN(-1);
}
}
*truncated = false;
DBUG_RETURN(sec_t + mrn_utc_diff_in_seconds);
}

long long int TimeConverter::tm_to_grn_time(struct tm *time, int usec,
bool *truncated) {
MRN_DBUG_ENTER_METHOD();

long long int grn_time;
long long int sec = tm_to_time_gm(time);
long long int sec = tm_to_time_gm(time, truncated);

DBUG_PRINT("info", ("mroonga: sec=%lld", sec));
DBUG_PRINT("info", ("mroonga: usec=%d", usec));

*truncated = false;
if (sec == -1) {
grn_time = 0;
*truncated = true;
} else {
grn_time = GRN_TIME_PACK(sec, usec);
}
long long int grn_time = *truncated ? 0 : GRN_TIME_PACK(sec, usec);

DBUG_RETURN(grn_time);
}
Expand Down
4 changes: 3 additions & 1 deletion lib/mrn_time_converter.hpp
Expand Up @@ -36,9 +36,11 @@ namespace mrn {
bool *truncated);

long long int tm_to_grn_time(struct tm *time, int usec, bool *truncated);
time_t tm_to_time_gm(struct tm *time);

void grn_time_to_mysql_time(long long int grn_time, MYSQL_TIME *mysql_time);

private:
time_t tm_to_time_gm(struct tm *time, bool *truncated);
};
}

Expand Down

0 comments on commit 1404bf0

Please sign in to comment.