From f073fe8ee5dcb0aa18c893198747062f2f51ab59 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 13 Oct 2022 14:12:39 -0700 Subject: Fix "unsafe narrowing" warnings in absl, 12/12. Addresses failures with the following, in some files: -Wshorten-64-to-32 -Wimplicit-int-conversion -Wsign-compare -Wsign-conversion -Wtautological-unsigned-zero-compare (This specific CL enables these warnings and fixes the remaining known issues.) Bug: chromium:1292951 PiperOrigin-RevId: 480981210 Change-Id: I92d5023c6833e24d6aa29b10d433116329972f41 --- absl/copts/GENERATED_AbseilCopts.cmake | 10 +++++++--- absl/copts/GENERATED_copts.bzl | 10 +++++++--- absl/copts/copts.py | 7 ++++--- absl/debugging/failure_signal_handler.cc | 3 ++- absl/debugging/internal/stacktrace_aarch64-inl.inc | 16 +++++++++------- absl/numeric/bits_benchmark.cc | 12 ++++++------ absl/random/internal/fast_uniform_bits.h | 3 ++- absl/random/internal/nonsecure_base.h | 2 +- absl/strings/charconv.cc | 8 ++++++-- absl/strings/internal/cord_internal.h | 2 +- 10 files changed, 45 insertions(+), 28 deletions(-) (limited to 'absl') diff --git a/absl/copts/GENERATED_AbseilCopts.cmake b/absl/copts/GENERATED_AbseilCopts.cmake index a4ab1aa2..38b57eec 100644 --- a/absl/copts/GENERATED_AbseilCopts.cmake +++ b/absl/copts/GENERATED_AbseilCopts.cmake @@ -15,13 +15,16 @@ list(APPEND ABSL_CLANG_CL_FLAGS list(APPEND ABSL_CLANG_CL_TEST_FLAGS "-Wno-c99-extensions" "-Wno-deprecated-declarations" + "-Wno-implicit-int-conversion" "-Wno-missing-noreturn" "-Wno-missing-prototypes" "-Wno-missing-variable-declarations" "-Wno-null-conversion" "-Wno-shadow" "-Wno-shift-sign-overflow" + "-Wno-shorten-64-to-32" "-Wno-sign-compare" + "-Wno-sign-conversion" "-Wno-unused-function" "-Wno-unused-member-function" "-Wno-unused-parameter" @@ -80,6 +83,7 @@ list(APPEND ABSL_LLVM_FLAGS "-Wshadow-all" "-Wstring-conversion" "-Wtautological-overlap-compare" + "-Wtautological-unsigned-zero-compare" "-Wundef" "-Wuninitialized" "-Wunreachable-code" @@ -91,9 +95,6 @@ list(APPEND ABSL_LLVM_FLAGS "-Wno-float-conversion" "-Wno-implicit-float-conversion" "-Wno-implicit-int-float-conversion" - "-Wno-implicit-int-conversion" - "-Wno-shorten-64-to-32" - "-Wno-sign-conversion" "-Wno-unknown-warning-option" "-DNOMINMAX" ) @@ -101,13 +102,16 @@ list(APPEND ABSL_LLVM_FLAGS list(APPEND ABSL_LLVM_TEST_FLAGS "-Wno-c99-extensions" "-Wno-deprecated-declarations" + "-Wno-implicit-int-conversion" "-Wno-missing-noreturn" "-Wno-missing-prototypes" "-Wno-missing-variable-declarations" "-Wno-null-conversion" "-Wno-shadow" "-Wno-shift-sign-overflow" + "-Wno-shorten-64-to-32" "-Wno-sign-compare" + "-Wno-sign-conversion" "-Wno-unused-function" "-Wno-unused-member-function" "-Wno-unused-parameter" diff --git a/absl/copts/GENERATED_copts.bzl b/absl/copts/GENERATED_copts.bzl index a6efc98e..fb0788a7 100644 --- a/absl/copts/GENERATED_copts.bzl +++ b/absl/copts/GENERATED_copts.bzl @@ -16,13 +16,16 @@ ABSL_CLANG_CL_FLAGS = [ ABSL_CLANG_CL_TEST_FLAGS = [ "-Wno-c99-extensions", "-Wno-deprecated-declarations", + "-Wno-implicit-int-conversion", "-Wno-missing-noreturn", "-Wno-missing-prototypes", "-Wno-missing-variable-declarations", "-Wno-null-conversion", "-Wno-shadow", "-Wno-shift-sign-overflow", + "-Wno-shorten-64-to-32", "-Wno-sign-compare", + "-Wno-sign-conversion", "-Wno-unused-function", "-Wno-unused-member-function", "-Wno-unused-parameter", @@ -81,6 +84,7 @@ ABSL_LLVM_FLAGS = [ "-Wshadow-all", "-Wstring-conversion", "-Wtautological-overlap-compare", + "-Wtautological-unsigned-zero-compare", "-Wundef", "-Wuninitialized", "-Wunreachable-code", @@ -92,9 +96,6 @@ ABSL_LLVM_FLAGS = [ "-Wno-float-conversion", "-Wno-implicit-float-conversion", "-Wno-implicit-int-float-conversion", - "-Wno-implicit-int-conversion", - "-Wno-shorten-64-to-32", - "-Wno-sign-conversion", "-Wno-unknown-warning-option", "-DNOMINMAX", ] @@ -102,13 +103,16 @@ ABSL_LLVM_FLAGS = [ ABSL_LLVM_TEST_FLAGS = [ "-Wno-c99-extensions", "-Wno-deprecated-declarations", + "-Wno-implicit-int-conversion", "-Wno-missing-noreturn", "-Wno-missing-prototypes", "-Wno-missing-variable-declarations", "-Wno-null-conversion", "-Wno-shadow", "-Wno-shift-sign-overflow", + "-Wno-shorten-64-to-32", "-Wno-sign-compare", + "-Wno-sign-conversion", "-Wno-unused-function", "-Wno-unused-member-function", "-Wno-unused-parameter", diff --git a/absl/copts/copts.py b/absl/copts/copts.py index 0d6c1ec3..8281d989 100644 --- a/absl/copts/copts.py +++ b/absl/copts/copts.py @@ -19,13 +19,16 @@ MSVC_BIG_WARNING_FLAGS = [ LLVM_TEST_DISABLE_WARNINGS_FLAGS = [ "-Wno-c99-extensions", "-Wno-deprecated-declarations", + "-Wno-implicit-int-conversion", "-Wno-missing-noreturn", "-Wno-missing-prototypes", "-Wno-missing-variable-declarations", "-Wno-null-conversion", "-Wno-shadow", "-Wno-shift-sign-overflow", + "-Wno-shorten-64-to-32", "-Wno-sign-compare", + "-Wno-sign-conversion", "-Wno-unused-function", "-Wno-unused-member-function", "-Wno-unused-parameter", @@ -96,6 +99,7 @@ COPT_VARS = { "-Wshadow-all", "-Wstring-conversion", "-Wtautological-overlap-compare", + "-Wtautological-unsigned-zero-compare", "-Wundef", "-Wuninitialized", "-Wunreachable-code", @@ -109,9 +113,6 @@ COPT_VARS = { "-Wno-float-conversion", "-Wno-implicit-float-conversion", "-Wno-implicit-int-float-conversion", - "-Wno-implicit-int-conversion", - "-Wno-shorten-64-to-32", - "-Wno-sign-conversion", # Disable warnings on unknown warning flags (when warning flags are # unknown on older compiler versions) "-Wno-unknown-warning-option", diff --git a/absl/debugging/failure_signal_handler.cc b/absl/debugging/failure_signal_handler.cc index 5e8f0b05..ef8ab9e5 100644 --- a/absl/debugging/failure_signal_handler.cc +++ b/absl/debugging/failure_signal_handler.cc @@ -138,7 +138,8 @@ static bool SetupAlternateStackOnce() { const size_t page_mask = static_cast(sysconf(_SC_PAGESIZE)) - 1; #endif size_t stack_size = - (std::max(SIGSTKSZ, 65536) + page_mask) & ~page_mask; + (std::max(static_cast(SIGSTKSZ), size_t{65536}) + page_mask) & + ~page_mask; #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ defined(ABSL_HAVE_MEMORY_SANITIZER) || defined(ABSL_HAVE_THREAD_SANITIZER) // Account for sanitizer instrumentation requiring additional stack space. diff --git a/absl/debugging/internal/stacktrace_aarch64-inl.inc b/absl/debugging/internal/stacktrace_aarch64-inl.inc index 891942c0..71cdaf09 100644 --- a/absl/debugging/internal/stacktrace_aarch64-inl.inc +++ b/absl/debugging/internal/stacktrace_aarch64-inl.inc @@ -19,7 +19,7 @@ #include "absl/debugging/internal/vdso_support.h" // a no-op on non-elf or non-glibc systems #include "absl/debugging/stacktrace.h" -static const uintptr_t kUnknownFrameSize = 0; +static const size_t kUnknownFrameSize = 0; #if defined(__linux__) // Returns the address of the VDSO __kernel_rt_sigreturn function, if present. @@ -65,11 +65,12 @@ static const unsigned char* GetKernelRtSigreturnAddress() { // Compute the size of a stack frame in [low..high). We assume that // low < high. Return size of kUnknownFrameSize. template -static inline uintptr_t ComputeStackFrameSize(const T* low, - const T* high) { +static inline size_t ComputeStackFrameSize(const T* low, + const T* high) { const char* low_char_ptr = reinterpret_cast(low); const char* high_char_ptr = reinterpret_cast(high); - return low < high ? high_char_ptr - low_char_ptr : kUnknownFrameSize; + return low < high ? static_cast(high_char_ptr - low_char_ptr) + : kUnknownFrameSize; } // Given a pointer to a stack frame, locate and return the calling @@ -117,8 +118,8 @@ static void **NextStackFrame(void **old_frame_pointer, const void *uc) { // Check frame size. In strict mode, we assume frames to be under // 100,000 bytes. In non-strict mode, we relax the limit to 1MB. if (check_frame_size) { - const uintptr_t max_size = STRICT_UNWINDING ? 100000 : 1000000; - const uintptr_t frame_size = + const size_t max_size = STRICT_UNWINDING ? 100000 : 1000000; + const size_t frame_size = ComputeStackFrameSize(old_frame_pointer, new_frame_pointer); if (frame_size == kUnknownFrameSize || frame_size > max_size) return nullptr; @@ -165,7 +166,8 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count, } else { result[n] = prev_return_address; if (IS_STACK_FRAMES) { - sizes[n] = ComputeStackFrameSize(frame_pointer, next_frame_pointer); + sizes[n] = static_cast( + ComputeStackFrameSize(frame_pointer, next_frame_pointer)); } n++; } diff --git a/absl/numeric/bits_benchmark.cc b/absl/numeric/bits_benchmark.cc index b9759583..719bfa81 100644 --- a/absl/numeric/bits_benchmark.cc +++ b/absl/numeric/bits_benchmark.cc @@ -25,17 +25,17 @@ namespace { template static void BM_bitwidth(benchmark::State& state) { - const int count = state.range(0); + const auto count = static_cast(state.range(0)); absl::BitGen rng; std::vector values; values.reserve(count); - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { values.push_back(absl::Uniform(rng, 0, std::numeric_limits::max())); } while (state.KeepRunningBatch(count)) { - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { benchmark::DoNotOptimize(values[i]); } } @@ -47,17 +47,17 @@ BENCHMARK_TEMPLATE(BM_bitwidth, uint64_t)->Range(1, 1 << 20); template static void BM_bitwidth_nonzero(benchmark::State& state) { - const int count = state.range(0); + const auto count = static_cast(state.range(0)); absl::BitGen rng; std::vector values; values.reserve(count); - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { values.push_back(absl::Uniform(rng, 1, std::numeric_limits::max())); } while (state.KeepRunningBatch(count)) { - for (int i = 0; i < count; ++i) { + for (size_t i = 0; i < count; ++i) { const T value = values[i]; ABSL_ASSUME(value > 0); benchmark::DoNotOptimize(value); diff --git a/absl/random/internal/fast_uniform_bits.h b/absl/random/internal/fast_uniform_bits.h index f3a5c00f..8d8ed045 100644 --- a/absl/random/internal/fast_uniform_bits.h +++ b/absl/random/internal/fast_uniform_bits.h @@ -151,7 +151,8 @@ FastUniformBits::Generate(URBG& g, // NOLINT(runtime/references) result_type r = static_cast(g() - kMin); for (size_t n = 1; n < kIters; ++n) { - r = (r << kShift) + static_cast(g() - kMin); + r = static_cast(r << kShift) + + static_cast(g() - kMin); } return r; } diff --git a/absl/random/internal/nonsecure_base.h b/absl/random/internal/nonsecure_base.h index c7d7fa4b..c3b80335 100644 --- a/absl/random/internal/nonsecure_base.h +++ b/absl/random/internal/nonsecure_base.h @@ -44,7 +44,7 @@ class RandenPoolSeedSeq { // Generate random unsigned values directly into the buffer. template void generate_impl(ContiguousTag, Contiguous begin, Contiguous end) { - const size_t n = std::distance(begin, end); + const size_t n = static_cast(std::distance(begin, end)); auto* a = &(*begin); RandenPool::Fill( absl::MakeSpan(reinterpret_cast(a), sizeof(*a) * n)); diff --git a/absl/strings/charconv.cc b/absl/strings/charconv.cc index c08623c4..69d420bc 100644 --- a/absl/strings/charconv.cc +++ b/absl/strings/charconv.cc @@ -298,7 +298,9 @@ struct CalculatedFloat { // minus the number of leading zero bits.) int BitWidth(uint128 value) { if (Uint128High64(value) == 0) { - return bit_width(Uint128Low64(value)); + // This static_cast is only needed when using a std::bit_width() + // implementation that does not have the fix for LWG 3656 applied. + return static_cast(bit_width(Uint128Low64(value))); } return 128 - countl_zero(Uint128High64(value)); } @@ -580,7 +582,9 @@ CalculatedFloat CalculateFromParsedHexadecimal( const strings_internal::ParsedFloat& parsed_hex) { uint64_t mantissa = parsed_hex.mantissa; int exponent = parsed_hex.exponent; - int mantissa_width = bit_width(mantissa); + // This static_cast is only needed when using a std::bit_width() + // implementation that does not have the fix for LWG 3656 applied. + int mantissa_width = static_cast(bit_width(mantissa)); const int shift = NormalizedShiftSize(mantissa_width, exponent); bool result_exact; exponent += shift; diff --git a/absl/strings/internal/cord_internal.h b/absl/strings/internal/cord_internal.h index eca747c8..fcca3a28 100644 --- a/absl/strings/internal/cord_internal.h +++ b/absl/strings/internal/cord_internal.h @@ -591,7 +591,7 @@ class InlineData { // See the documentation on 'as_chars()' for more information and examples. void set_inline_size(size_t size) { ABSL_ASSERT(size <= kMaxInline); - tag() = static_cast(size << 1); + tag() = static_cast(size << 1); } // Compares 'this' inlined data with rhs. The comparison is a straightforward -- cgit v1.2.3