From 344088dc11b56d2f0c300743c4a89c2cafc6a2b5 Mon Sep 17 00:00:00 2001 From: Christopher Dykes Date: Mon, 12 Dec 2016 15:15:27 -0800 Subject: [PATCH] Allow building with -Wshift-sign-overflow Summary: Prior to C++14 these shifts are undefined behavior, but the unsigned version is not, so do the shifts on unsigned values before converting to the final type. Reviewed By: yfeldblum Differential Revision: D4309311 fbshipit-source-id: 914b207bac2f77a96c07a8a5df81980c672aa677 --- folly/test/JsonTest.cpp | 4 ++-- folly/test/SmallLocksTest.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/folly/test/JsonTest.cpp b/folly/test/JsonTest.cpp index 805cb300..bd11be75 100644 --- a/folly/test/JsonTest.cpp +++ b/folly/test/JsonTest.cpp @@ -154,14 +154,14 @@ TEST(Json, BoolConversion) { } TEST(Json, JavascriptSafe) { - auto badDouble = (1ll << 63ll) + 1; + auto badDouble = int64_t((1ULL << 63ULL) + 1); dynamic badDyn = badDouble; EXPECT_EQ(folly::toJson(badDouble), folly::to(badDouble)); folly::json::serialization_opts opts; opts.javascript_safe = true; EXPECT_ANY_THROW(folly::json::serialize(badDouble, opts)); - auto okDouble = 1ll << 63ll; + auto okDouble = int64_t(1ULL << 63ULL); dynamic okDyn = okDouble; EXPECT_EQ(folly::toJson(okDouble), folly::to(okDouble)); } diff --git a/folly/test/SmallLocksTest.cpp b/folly/test/SmallLocksTest.cpp index 5bb9ddb5..0c24664b 100644 --- a/folly/test/SmallLocksTest.cpp +++ b/folly/test/SmallLocksTest.cpp @@ -91,7 +91,8 @@ template struct PslTest { PslTest() { lock.init(); } void doTest() { - T ourVal = rand() % (T(1) << (sizeof(T) * 8 - 1)); + using UT = typename std::make_unsigned::type; + T ourVal = rand() % T(UT(1) << (sizeof(UT) * 8 - 1)); for (int i = 0; i < 10000; ++i) { std::lock_guard> guard(lock); lock.setData(ourVal); -- 2.34.1