From: Marcin Siodelski Date: Wed, 4 Mar 2015 11:50:26 +0000 (+0100) Subject: [3673] Use the 64-bit value for time conversions to detect overflows. X-Git-Tag: trac3764_base~16^2~10 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=829ad6fa6924f8e68b130842f5d1079b57729028;p=thirdparty%2Fkea.git [3673] Use the 64-bit value for time conversions to detect overflows. --- diff --git a/src/lib/dhcpsrv/pgsql_lease_mgr.cc b/src/lib/dhcpsrv/pgsql_lease_mgr.cc index e337f0a22d..fe8addcecb 100644 --- a/src/lib/dhcpsrv/pgsql_lease_mgr.cc +++ b/src/lib/dhcpsrv/pgsql_lease_mgr.cc @@ -298,20 +298,23 @@ public: /// when stored. Likewise, these columns are automatically adjusted /// upon retrieval unless fetched via "extract(epoch from ))". /// - /// @param time_val timestamp to be converted + /// @param time_val_64 timestamp to be converted. This is given as a + /// 64-bit value to avoid overflows on the 32-bit systems where time_t + /// is implemented as int32_t. /// @return std::string containing the stringified time std::string - convertToDatabaseTime(const time_t& time_val) { + convertToDatabaseTime(const int64_t& time_val_64) { // PostgreSQL does funny things with time if you get past Y2038. It // will accept the values (unlike MySQL which throws) but it // stops correctly adjusting to local time when reading them back // out. So lets disallow it here. - if (time_val > LeaseMgr::MAX_DB_TIME) { - isc_throw(BadValue, "Time value is too large: " << time_val); + if (time_val_64 > LeaseMgr::MAX_DB_TIME) { + isc_throw(BadValue, "Time value is too large: " << time_val_64); } struct tm tinfo; char buffer[20]; + const time_t time_val = static_cast(time_val_64); localtime_r(&time_val, &tinfo); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tinfo); return (std::string(buffer)); @@ -632,8 +635,8 @@ public: (lease->valid_lft_); bind_array.add(valid_lft_str_); - expire_str_ = convertToDatabaseTime(lease->valid_lft_ + - lease->cltt_); + expire_str_ = convertToDatabaseTime(static_cast(lease->valid_lft_) + + static_cast(lease->cltt_)); bind_array.add(expire_str_); subnet_id_str_ = boost::lexical_cast @@ -796,8 +799,8 @@ public: (lease->valid_lft_); bind_array.add(valid_lft_str_); - expire_str_ = convertToDatabaseTime(lease->valid_lft_ + - lease->cltt_); + expire_str_ = convertToDatabaseTime(static_cast(lease->valid_lft_) + + static_cast(lease->cltt_)); bind_array.add(expire_str_); subnet_id_str_ = boost::lexical_cast