Fix data race in folly/executors/Codel.cpp exposed by TSAN
[folly.git] / folly / Format.cpp
index 11f87e9da61e64617d04fdd440c5794fa24d063e..bbcd2d004dec69f155331f26652c8ab3d963e616 100644 (file)
@@ -16,7 +16,8 @@
 
 #include <folly/Format.h>
 
-#include <folly/portability/Constexpr.h>
+#include <folly/ConstexprMath.h>
+#include <folly/CppAttributes.h>
 
 #include <double-conversion/double-conversion.h>
 
@@ -26,7 +27,7 @@ namespace detail {
 extern const FormatArg::Align formatAlignTable[];
 extern const FormatArg::Sign formatSignTable[];
 
-}  // namespace detail
+} // namespace detail
 
 using namespace folly::detail;
 
@@ -50,14 +51,14 @@ void FormatValue<double>::formatHelper(
   }
 
   // 2+: for null terminator and optional sign shenanigans.
-  constexpr size_t bufLen =
+  constexpr int bufLen =
       2 + constexpr_max(
               2 + DoubleToStringConverter::kMaxFixedDigitsBeforePoint +
                   DoubleToStringConverter::kMaxFixedDigitsAfterPoint,
               constexpr_max(8 + DoubleToStringConverter::kMaxExponentialDigits,
                             7 + DoubleToStringConverter::kMaxPrecisionDigits));
   char buf[bufLen];
-  StringBuilder builder(buf + 1, static_cast<int> (sizeof(buf) - 1));
+  StringBuilder builder(buf + 1, bufLen - 1);
 
   char plusSign;
   switch (arg.sign) {
@@ -81,6 +82,7 @@ void FormatValue<double>::formatHelper(
   switch (arg.presentation) {
   case '%':
     val *= 100;
+    FOLLY_FALLTHROUGH;
   case 'f':
   case 'F':
     {
@@ -159,7 +161,7 @@ void FormatValue<double>::formatHelper(
     prefixLen = 1;
   }
 
-  piece = fbstring(p, len);
+  piece = fbstring(p, size_t(len));
 }
 
 
@@ -326,6 +328,6 @@ void insertThousandsGroupingUnsafe(char* start_buffer, char** end_buffer) {
     remaining_digits -= current_group_size;
   }
 }
-} // detail
+} // namespace detail
 
-}  // namespace folly
+} // namespace folly