]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#2532] Fixed 2038 vs postgreSQL issue
authorFrancis Dupont <fdupont@isc.org>
Wed, 31 Aug 2022 13:36:21 +0000 (15:36 +0200)
committerFrancis Dupont <fdupont@isc.org>
Fri, 23 Sep 2022 14:01:36 +0000 (16:01 +0200)
src/lib/pgsql/pgsql_exchange.cc
src/lib/pgsql/tests/pgsql_exchange_unittest.cc

index a646c46fb1b3d164bbcf6ff08967ef4a4b44a137..d203714108638f0e5fc89eb7de311363e064a955 100644 (file)
@@ -239,11 +239,17 @@ PsqlBindArray::addTimestamp(const boost::posix_time::ptime& timestamp) {
     // Sadly boost::posix_time::to_time_t() was not added until 1.58,
     // so do it ourselves.
     ptime epoch(boost::gregorian::date(1970, 1, 1));
+    if (timestamp < epoch) {
+        isc_throw(isc::BadValue, "Time value is before the epoch");
+    }
+    ptime max_db_time = boost::posix_time::from_time_t(DatabaseConnection::MAX_DB_TIME);
     time_duration::sec_type since_epoch = (timestamp - epoch).total_seconds();
     time_t input_time(since_epoch);
-
-    if (input_time > DatabaseConnection::MAX_DB_TIME) {
-        isc_throw(isc::BadValue, "Time value is too large: " << input_time);
+    if (timestamp > max_db_time) {
+        isc_throw(isc::BadValue, "Time value is too large: " <<
+                  (input_time < 0 ?
+                   static_cast<int64_t>(static_cast<uint32_t>(input_time)) :
+                   input_time));
     }
 
     // Converts to timestamp to local date/time string.
index fe5dfddfff52e1d41e029f3cebc06a903cd4576c..7bbf747d8f791cd66159b8c9ef76f574aa1dbaba 100644 (file)
@@ -977,6 +977,11 @@ TEST_F(PgSqlBasicsTest, ptimeTimestamp) {
     // Create an empty array.
     PsqlBindArrayPtr bind_array(new PsqlBindArray());
 
+    // Make sure we catch values before the epoch.
+    ptime christmas1969(date(1969, Dec, 25));
+    ASSERT_THROW_MSG(bind_array->addTimestamp(christmas1969), BadValue,
+                     "Time value is before the epoch");
+
     // Make sure we catch values that are too big.
     time_duration duration = hours(10) + minutes(14) + seconds(15);
     ptime day_too_far(date(2038, Jan, 21), duration);