From dc28381834ab1c9cae55f6d3bf3a122335220ab0 Mon Sep 17 00:00:00 2001 From: Richard Barnes Date: Sun, 22 Sep 2024 19:06:55 -0700 Subject: [PATCH] Use `auto` types in folly to address `-Wshorten-64-to-32` Summary: To make things easier to review, I've moved the auto types over here. Reviewed By: Gownta Differential Revision: D57686669 fbshipit-source-id: cbc89c9a9cce5850e0f5f30a769ae410e563821d --- folly/Benchmark.cpp | 2 +- folly/Subprocess.cpp | 2 +- .../debugging/symbolizer/SymbolizePrinter.cpp | 2 +- folly/detail/Futex.cpp | 4 ++-- folly/io/async/SSLContext.cpp | 10 +++++----- .../io/async/test/AsyncUDPSocketGSOGROTest.cpp | 2 +- folly/io/async/test/EventBaseTestLib.h | 4 ++-- folly/io/async/test/EventHandlerTest.cpp | 18 +++++++++--------- folly/io/async/test/SSLContextTest.cpp | 3 ++- folly/io/async/test/TimeUtil.cpp | 12 ++++++------ folly/io/test/IOBufTest.cpp | 2 +- folly/test/ConvBenchmark.cpp | 4 ++-- folly/test/stl_tests/StlVectorTest.cpp | 10 +++++----- 13 files changed, 38 insertions(+), 37 deletions(-) diff --git a/folly/Benchmark.cpp b/folly/Benchmark.cpp index 63b84c526de..180a4162ad8 100644 --- a/folly/Benchmark.cpp +++ b/folly/Benchmark.cpp @@ -410,7 +410,7 @@ void printDefaultHeaderContents(std::string_view file, size_t columns) { } else { std::string truncatedFile = std::string(file.begin(), file.end()); constexpr std::string_view overflowFilePrefix = "[...]"; - const int overflow = truncatedFile.size() - maxFileNameChars; + const auto overflow = truncatedFile.size() - maxFileNameChars; truncatedFile.erase(0, overflow); truncatedFile.replace(0, overflowFilePrefix.size(), overflowFilePrefix); printHeaderContents(truncatedFile); diff --git a/folly/Subprocess.cpp b/folly/Subprocess.cpp index 14732ada638..5183e681ea3 100644 --- a/folly/Subprocess.cpp +++ b/folly/Subprocess.cpp @@ -603,7 +603,7 @@ void Subprocess::closeInheritedFds(const Options::FdMap& fdActions) { #endif // If not running on Linux or if we failed to open /proc/self/fd, try to close // all possible open file descriptors. - for (int fd = sysconf(_SC_OPEN_MAX) - 1; fd >= 3; --fd) { + for (auto fd = sysconf(_SC_OPEN_MAX) - 1; fd >= 3; --fd) { if (fdActions.count(fd) == 0) { ::close(fd); } diff --git a/folly/debugging/symbolizer/SymbolizePrinter.cpp b/folly/debugging/symbolizer/SymbolizePrinter.cpp index 95b34678c5e..56be731841b 100644 --- a/folly/debugging/symbolizer/SymbolizePrinter.cpp +++ b/folly/debugging/symbolizer/SymbolizePrinter.cpp @@ -112,7 +112,7 @@ void SymbolizePrinter::print(const SymbolizedFrame& frame) { doPrint(fileBuf); char buf[to_ascii_size_max_decimal]; - uint32_t n = to_ascii_decimal(buf, frame.location.line); + const auto n = to_ascii_decimal(buf, frame.location.line); doPrint(":"); doPrint(StringPiece(buf, n)); } else { diff --git a/folly/detail/Futex.cpp b/folly/detail/Futex.cpp index 3b9fc58283c..f76b427b0e0 100644 --- a/folly/detail/Futex.cpp +++ b/folly/detail/Futex.cpp @@ -61,7 +61,7 @@ namespace { #endif int nativeFutexWake(const void* addr, int count, uint32_t wakeMask) { - int rv = syscall( + const auto rv = syscall( __NR_futex, addr, /* addr1 */ FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG, /* op */ @@ -123,7 +123,7 @@ FutexResult nativeFutexWaitImpl( // Unlike FUTEX_WAIT, FUTEX_WAIT_BITSET requires an absolute timeout // value - http://locklessinc.com/articles/futex_cheat_sheet/ - int rv = syscall( + const auto rv = syscall( __NR_futex, addr, /* addr1 */ op, /* op */ diff --git a/folly/io/async/SSLContext.cpp b/folly/io/async/SSLContext.cpp index 90ee02e6694..a42cbdb2b1a 100644 --- a/folly/io/async/SSLContext.cpp +++ b/folly/io/async/SSLContext.cpp @@ -70,7 +70,7 @@ void configureProtocolVersion(SSL_CTX* ctx, SSLContext::SSLVersion version) { // do nothing break; } - int setMinProtoResult = SSL_CTX_set_min_proto_version(ctx, minVersion); + const auto setMinProtoResult = SSL_CTX_set_min_proto_version(ctx, minVersion); DCHECK(setMinProtoResult == 1) << sformat("unsupported min TLS protocol version: 0x{:04x}", minVersion); } @@ -141,7 +141,7 @@ void SSLContext::setClientECCurvesList( } std::string ecCurvesList; join(":", ecCurves, ecCurvesList); - int rc = SSL_CTX_set1_curves_list(ctx_, ecCurvesList.c_str()); + const auto rc = SSL_CTX_set1_curves_list(ctx_, ecCurvesList.c_str()); if (rc == 0) { throw std::runtime_error("SSL_CTX_set1_curves_list " + getErrors()); } @@ -153,7 +153,7 @@ void SSLContext::setSupportedGroups(const std::vector& groups) { } std::string groupsList; join(":", groups, groupsList); - int rc = SSL_CTX_set1_groups_list(ctx_, groupsList.c_str()); + const auto rc = SSL_CTX_set1_groups_list(ctx_, groupsList.c_str()); if (rc == 0) { throw std::runtime_error("SSL_CTX_set1_curves " + getErrors()); } @@ -201,7 +201,7 @@ void SSLContext::setX509VerifyParam( } void SSLContext::setCiphersOrThrow(const std::string& ciphers) { - int rc = SSL_CTX_set_cipher_list(ctx_, ciphers.c_str()); + const auto rc = SSL_CTX_set_cipher_list(ctx_, ciphers.c_str()); if (rc == 0) { throw std::runtime_error("SSL_CTX_set_cipher_list: " + getErrors()); } @@ -209,7 +209,7 @@ void SSLContext::setCiphersOrThrow(const std::string& ciphers) { } void SSLContext::setSigAlgsOrThrow(const std::string& sigalgs) { - int rc = SSL_CTX_set1_sigalgs_list(ctx_, sigalgs.c_str()); + const auto rc = SSL_CTX_set1_sigalgs_list(ctx_, sigalgs.c_str()); if (rc == 0) { throw std::runtime_error("SSL_CTX_set1_sigalgs_list " + getErrors()); } diff --git a/folly/io/async/test/AsyncUDPSocketGSOGROTest.cpp b/folly/io/async/test/AsyncUDPSocketGSOGROTest.cpp index 63487a0bebd..72935c00e6e 100644 --- a/folly/io/async/test/AsyncUDPSocketGSOGROTest.cpp +++ b/folly/io/async/test/AsyncUDPSocketGSOGROTest.cpp @@ -169,7 +169,7 @@ class UDPAcceptor : public AsyncUDPServerSocket::Callback { if (params.gro == -1) { socket->write(client, data->clone()); } else { - int total = data->length(); + int64_t total = data->length(); size_t offset = 0; while (total > 0) { auto size = (total > params.gro) ? params.gro : total; diff --git a/folly/io/async/test/EventBaseTestLib.h b/folly/io/async/test/EventBaseTestLib.h index dba4d8077ed..dbfa3f6a798 100644 --- a/folly/io/async/test/EventBaseTestLib.h +++ b/folly/io/async/test/EventBaseTestLib.h @@ -80,7 +80,7 @@ FOLLY_ALWAYS_INLINE ssize_t writeToFD(int fd, size_t length) { auto bufv = std::vector(length); auto buf = bufv.data(); memset(buf, 'a', length); - ssize_t rc = write(fd, buf, length); + const auto rc = write(fd, buf, length); CHECK_EQ(rc, length); return rc; } @@ -113,7 +113,7 @@ FOLLY_ALWAYS_INLINE size_t readUntilEmpty(int fd) { char buf[BUF_SIZE]; size_t bytesRead = 0; while (true) { - int rc = read(fd, buf, sizeof(buf)); + const auto rc = read(fd, buf, sizeof(buf)); if (rc == 0) { CHECK(false) << "unexpected EOF"; } else if (rc < 0) { diff --git a/folly/io/async/test/EventHandlerTest.cpp b/folly/io/async/test/EventHandlerTest.cpp index fce58df0ebb..34486787745 100644 --- a/folly/io/async/test/EventHandlerTest.cpp +++ b/folly/io/async/test/EventHandlerTest.cpp @@ -278,7 +278,7 @@ class EventHandlerOobTest : public ::testing::Test { TEST_F(EventHandlerOobTest, EPOLLPRI) { auto clientOps = [](int fd) { char buffer[] = "banana"; - int n = send(fd, buffer, strlen(buffer) + 1, MSG_OOB); + const auto n = send(fd, buffer, strlen(buffer) + 1, MSG_OOB); LOG(INFO) << "Client send finished"; PCHECK(n > 0); }; @@ -293,7 +293,7 @@ TEST_F(EventHandlerOobTest, EPOLLPRI) { void handlerReady(uint16_t events) noexcept override { EXPECT_TRUE(EventHandler::EventFlags::PRI & events); std::array buffer; - int n = read(fd_, buffer.data(), buffer.size()); + auto n = read(fd_, buffer.data(), buffer.size()); // // NB: we sent 7 bytes, but only received 6. The last byte // has been stored in the OOB buffer. @@ -322,13 +322,13 @@ TEST_F(EventHandlerOobTest, OOB_AND_NORMAL_DATA) { { // OOB buffer can only hold one byte in most implementations std::array buffer = {"X"}; - int n = send(sockfd, buffer.data(), 1, MSG_OOB); + const auto n = send(sockfd, buffer.data(), 1, MSG_OOB); PCHECK(n > 0); } { std::array buffer = {"banana"}; - int n = send(sockfd, buffer.data(), buffer.size(), 0); + const auto n = send(sockfd, buffer.data(), buffer.size(), 0); PCHECK(n > 0); } }; @@ -343,7 +343,7 @@ TEST_F(EventHandlerOobTest, OOB_AND_NORMAL_DATA) { void handlerReady(uint16_t events) noexcept override { std::array buffer; if (events & EventHandler::EventFlags::PRI) { - int n = recv(fd_, buffer.data(), buffer.size(), MSG_OOB); + const auto n = recv(fd_, buffer.data(), buffer.size(), MSG_OOB); EXPECT_EQ(1, n); EXPECT_EQ("X", std::string(buffer.data(), 1)); registerHandler(EventHandler::EventFlags::READ); @@ -351,7 +351,7 @@ TEST_F(EventHandlerOobTest, OOB_AND_NORMAL_DATA) { } if (events & EventHandler::EventFlags::READ) { - int n = recv(fd_, buffer.data(), buffer.size(), 0); + const auto n = recv(fd_, buffer.data(), buffer.size(), 0); EXPECT_EQ(7, n); EXPECT_EQ("banana", std::string(buffer.data())); eb_->terminateLoopSoon(); @@ -376,13 +376,13 @@ TEST_F(EventHandlerOobTest, SWALLOW_OOB) { auto clientOps = [](int sockfd) { { std::array buffer = {"X"}; - int n = send(sockfd, buffer.data(), 1, MSG_OOB); + const auto n = send(sockfd, buffer.data(), 1, MSG_OOB); PCHECK(n > 0); } { std::array buffer = {"banana"}; - int n = send(sockfd, buffer.data(), buffer.size(), 0); + const auto n = send(sockfd, buffer.data(), buffer.size(), 0); PCHECK(n > 0); } }; @@ -397,7 +397,7 @@ TEST_F(EventHandlerOobTest, SWALLOW_OOB) { void handlerReady(uint16_t events) noexcept override { std::array buffer; ASSERT_TRUE(events & EventHandler::EventFlags::READ); - int n = recv(fd_, buffer.data(), buffer.size(), 0); + const auto n = recv(fd_, buffer.data(), buffer.size(), 0); EXPECT_EQ(7, n); EXPECT_EQ("banana", std::string(buffer.data())); } diff --git a/folly/io/async/test/SSLContextTest.cpp b/folly/io/async/test/SSLContextTest.cpp index b16ecdc6442..ab889c566a2 100644 --- a/folly/io/async/test/SSLContextTest.cpp +++ b/folly/io/async/test/SSLContextTest.cpp @@ -303,7 +303,8 @@ TEST_F(SSLContextTest, TestSetInvalidCiphersuite) { TEST_F(SSLContextTest, TestTLS13MinVersion) { SSLContext sslContext{SSLContext::SSLVersion::TLSv1_3}; - int minProtoVersion = SSL_CTX_get_min_proto_version(sslContext.getSSLCtx()); + const auto minProtoVersion = + SSL_CTX_get_min_proto_version(sslContext.getSSLCtx()); EXPECT_EQ(minProtoVersion, TLS1_3_VERSION); } diff --git a/folly/io/async/test/TimeUtil.cpp b/folly/io/async/test/TimeUtil.cpp index ed2366065b9..a5e7084d904 100644 --- a/folly/io/async/test/TimeUtil.cpp +++ b/folly/io/async/test/TimeUtil.cpp @@ -49,20 +49,20 @@ namespace folly { #ifdef __linux__ static int getLinuxVersion(StringPiece release) { - auto dot1 = release.find('.'); + const auto dot1 = release.find('.'); if (dot1 == StringPiece::npos) { throw std::invalid_argument("could not find first dot"); } - auto v1 = folly::to(release.subpiece(0, dot1)); + const auto v1 = folly::to(release.subpiece(0, dot1)); - auto dot2 = release.find('.', dot1 + 1); + const auto dot2 = release.find('.', dot1 + 1); if (dot2 == StringPiece::npos) { throw std::invalid_argument("could not find second dot"); } - auto v2 = folly::to(release.subpiece(dot1 + 1, dot2 - (dot1 + 1))); + const auto v2 = folly::to(release.subpiece(dot1 + 1, dot2 - (dot1 + 1))); - int dash = release.find('-', dot2 + 1); - auto v3 = folly::to(release.subpiece(dot2 + 1, dash - (dot2 + 1))); + const auto dash = release.find('-', dot2 + 1); + const auto v3 = folly::to(release.subpiece(dot2 + 1, dash - (dot2 + 1))); return ((v1 * 1000 + v2) * 1000) + v3; } diff --git a/folly/io/test/IOBufTest.cpp b/folly/io/test/IOBufTest.cpp index e80ea879296..a4b3a0290b0 100644 --- a/folly/io/test/IOBufTest.cpp +++ b/folly/io/test/IOBufTest.cpp @@ -46,7 +46,7 @@ void prepend(std::unique_ptr& buf, StringPiece str) { TEST(IOBuf, Simple) { unique_ptr buf(IOBuf::create(100)); - uint32_t cap = buf->capacity(); + const auto cap = buf->capacity(); EXPECT_LE(100, cap); EXPECT_EQ(0, buf->headroom()); EXPECT_EQ(0, buf->length()); diff --git a/folly/test/ConvBenchmark.cpp b/folly/test/ConvBenchmark.cpp index 9495861c109..9e02bbda14c 100644 --- a/folly/test/ConvBenchmark.cpp +++ b/folly/test/ConvBenchmark.cpp @@ -347,8 +347,8 @@ unsigned u64ToAsciiTable(uint64_t value, char* dst) { "80818283848586878889" "90919293949596979899"; - uint32_t const length = to_ascii_size_decimal(value); - uint32_t next = length - 1; + auto const length = to_ascii_size_decimal(value); + auto next = length - 1; while (value >= 100) { auto const i = (value % 100) * 2; value /= 100; diff --git a/folly/test/stl_tests/StlVectorTest.cpp b/folly/test/stl_tests/StlVectorTest.cpp index 3a078b78b55..61d1e55f7ce 100644 --- a/folly/test/stl_tests/StlVectorTest.cpp +++ b/folly/test/stl_tests/StlVectorTest.cpp @@ -2325,10 +2325,10 @@ STL_TEST( a, p) { DataState dsa(a); - int idx = distance(a.begin(), p); - auto am = a.get_allocator(); + const auto idx = distance(a.begin(), p); + const auto am = a.get_allocator(); - auto q = a.emplace(p, 44); + const auto q = a.emplace(p, 44); ASSERT_TRUE(am == a.get_allocator()); ASSERT_EQ(idx, distance(a.begin(), q)) << "incorrect iterator returned"; @@ -2349,12 +2349,12 @@ STL_TEST( SKIP(); } DataState dsa(a); - int idx = distance(a.begin(), p); + const auto idx = distance(a.begin(), p); int tval = convertToInt(t); auto am = a.get_allocator(); const auto& ct = t; - auto q = a.insert(p, ct); + const auto q = a.insert(p, ct); ASSERT_TRUE(am == a.get_allocator()); ASSERT_EQ(idx, distance(a.begin(), q)) << "incorrect iterator returned";