DBWithTTLImpl::IsStale overflow when ttl is 15 years (#11279)

Summary:
Fix DBWIthTTLImpl::IsStale overflow

Pull Request resolved: https://github.com/facebook/rocksdb/pull/11279

Reviewed By: cbi42

Differential Revision: D43875039

Pulled By: ajkr

fbshipit-source-id: 3e5feb8c4c4480bf1421b0763ade3d2e459ec028
oxigraph-8.1.1
zhangliangkai1992 2 years ago committed by Facebook GitHub Bot
parent daeec505a4
commit 7a07afe82e
  1. 6
      utilities/ttl/db_ttl_impl.cc
  2. 11
      utilities/ttl/ttl_test.cc

@ -451,7 +451,11 @@ bool DBWithTTLImpl::IsStale(const Slice& value, int32_t ttl,
if (!clock->GetCurrentTime(&curtime).ok()) { if (!clock->GetCurrentTime(&curtime).ok()) {
return false; // Treat the data as fresh if could not get current time 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); DecodeFixed32(value.data() + value.size() - kTSLength);
return (timestamp_value + ttl) < curtime; return (timestamp_value + ttl) < curtime;
} }

@ -635,6 +635,17 @@ TEST_F(TtlTest, MultiGetTest) {
CloseTtl(); 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) { TEST_F(TtlTest, ColumnFamiliesTest) {
DB* db; DB* db;
Options options; Options options;

Loading…
Cancel
Save