diff --git a/utilities/ttl/db_ttl_impl.cc b/utilities/ttl/db_ttl_impl.cc index c38efa5fe..5b0486fc1 100644 --- a/utilities/ttl/db_ttl_impl.cc +++ b/utilities/ttl/db_ttl_impl.cc @@ -451,7 +451,11 @@ bool DBWithTTLImpl::IsStale(const Slice& value, int32_t ttl, if (!clock->GetCurrentTime(&curtime).ok()) { return false; // Treat the data as fresh if could not get current time } - int32_t timestamp_value = + /* int32_t may overflow when timestamp_value + ttl + * for example ttl = 86400 * 365 * 15 + * convert timestamp_value to int64_t + */ + int64_t timestamp_value = DecodeFixed32(value.data() + value.size() - kTSLength); return (timestamp_value + ttl) < curtime; } diff --git a/utilities/ttl/ttl_test.cc b/utilities/ttl/ttl_test.cc index 23cb23caf..225db59b5 100644 --- a/utilities/ttl/ttl_test.cc +++ b/utilities/ttl/ttl_test.cc @@ -635,6 +635,17 @@ TEST_F(TtlTest, MultiGetTest) { CloseTtl(); } +TEST_F(TtlTest, TtlFiftenYears) { + MakeKVMap(kSampleSize_); + // 15 year will lead int32_t overflow from now + const int kFifteenYearSeconds = 86400 * 365 * 15; + OpenTtl(kFifteenYearSeconds); + PutValues(0, kSampleSize_, true); + // trigger the compaction + SleepCompactCheck(1, 0, kSampleSize_); + CloseTtl(); +} + TEST_F(TtlTest, ColumnFamiliesTest) { DB* db; Options options;