]> git-server-git.apps.pok.os.sepia.ceph.com Git - rocksdb.git/commitdiff
Avoid intentional overflow in GetL0ThresholdSpeedupCompaction
authorIslam AbdelRahman <tec@fb.com>
Tue, 29 Nov 2016 02:25:27 +0000 (18:25 -0800)
committerIslam AbdelRahman <tec@fb.com>
Tue, 29 Nov 2016 02:48:57 +0000 (18:48 -0800)
Summary:
https://github.com/facebook/rocksdb/commit/99c052a34f93d119b75eccdcd489ecd581d48ee9 fixes integer overflow in GetL0ThresholdSpeedupCompaction() by checking if int become -ve.
UBSAN will complain about that since this is still an overflow, we can fix the issue by simply using int64_t
Closes https://github.com/facebook/rocksdb/pull/1582

Differential Revision: D4241525

Pulled By: IslamAbdelRahman

fbshipit-source-id: b3ae21f

db/column_family.cc

index 540ed74f0749e2e9c0f28c3fb10af99cc699ef51..1ddd19fe103228839091c444e0ef6de3cb0ff66d 100644 (file)
@@ -553,30 +553,26 @@ int GetL0ThresholdSpeedupCompaction(int level0_file_num_compaction_trigger,
     return std::numeric_limits<int>::max();
   }
 
-  const int twice_level0_trigger = level0_file_num_compaction_trigger * 2;
+  const int64_t twice_level0_trigger =
+      static_cast<int64_t>(level0_file_num_compaction_trigger) * 2;
 
-  // overflow protection
-  if (twice_level0_trigger < 0) {
-    return std::numeric_limits<int>::max();
-  }
-
-  // 1/4 of the way between L0 compaction trigger threshold and slowdown
-  // condition.
-  const int one_fourth_trigger_slowdown =
-      level0_file_num_compaction_trigger +
+  const int64_t one_fourth_trigger_slowdown =
+      static_cast<int64_t>(level0_file_num_compaction_trigger) +
       ((level0_slowdown_writes_trigger - level0_file_num_compaction_trigger) /
        4);
 
-  // overflow protection
-  if (one_fourth_trigger_slowdown < 0) {
-    return std::numeric_limits<int>::max();
-  }
-
   assert(twice_level0_trigger >= 0);
   assert(one_fourth_trigger_slowdown >= 0);
 
+  // 1/4 of the way between L0 compaction trigger threshold and slowdown
+  // condition.
   // Or twice as compaction trigger, if it is smaller.
-  return std::min(twice_level0_trigger, one_fourth_trigger_slowdown);
+  int64_t res = std::min(twice_level0_trigger, one_fourth_trigger_slowdown);
+  if (res >= port::kMaxInt32) {
+    return port::kMaxInt32;
+  } else {
+    return res;
+  }
 }
 }  // namespace