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
pull/1298/head
Abseil Team 2 years ago committed by Copybara-Service
parent cbaba1a9bf
commit f073fe8ee5
  1. 10
      absl/copts/GENERATED_AbseilCopts.cmake
  2. 10
      absl/copts/GENERATED_copts.bzl
  3. 7
      absl/copts/copts.py
  4. 3
      absl/debugging/failure_signal_handler.cc
  5. 16
      absl/debugging/internal/stacktrace_aarch64-inl.inc
  6. 12
      absl/numeric/bits_benchmark.cc
  7. 3
      absl/random/internal/fast_uniform_bits.h
  8. 2
      absl/random/internal/nonsecure_base.h
  9. 8
      absl/strings/charconv.cc
  10. 2
      absl/strings/internal/cord_internal.h

@ -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"

@ -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",

@ -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",

@ -138,7 +138,8 @@ static bool SetupAlternateStackOnce() {
const size_t page_mask = static_cast<size_t>(sysconf(_SC_PAGESIZE)) - 1;
#endif
size_t stack_size =
(std::max<size_t>(SIGSTKSZ, 65536) + page_mask) & ~page_mask;
(std::max(static_cast<size_t>(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.

@ -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<typename T>
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<const char *>(low);
const char* high_char_ptr = reinterpret_cast<const char *>(high);
return low < high ? high_char_ptr - low_char_ptr : kUnknownFrameSize;
return low < high ? static_cast<size_t>(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<int>(
ComputeStackFrameSize(frame_pointer, next_frame_pointer));
}
n++;
}

@ -25,17 +25,17 @@ namespace {
template <typename T>
static void BM_bitwidth(benchmark::State& state) {
const int count = state.range(0);
const auto count = static_cast<size_t>(state.range(0));
absl::BitGen rng;
std::vector<T> values;
values.reserve(count);
for (int i = 0; i < count; ++i) {
for (size_t i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 0, std::numeric_limits<T>::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 <typename T>
static void BM_bitwidth_nonzero(benchmark::State& state) {
const int count = state.range(0);
const auto count = static_cast<size_t>(state.range(0));
absl::BitGen rng;
std::vector<T> values;
values.reserve(count);
for (int i = 0; i < count; ++i) {
for (size_t i = 0; i < count; ++i) {
values.push_back(absl::Uniform<T>(rng, 1, std::numeric_limits<T>::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);

@ -151,7 +151,8 @@ FastUniformBits<UIntType>::Generate(URBG& g, // NOLINT(runtime/references)
result_type r = static_cast<result_type>(g() - kMin);
for (size_t n = 1; n < kIters; ++n) {
r = (r << kShift) + static_cast<result_type>(g() - kMin);
r = static_cast<result_type>(r << kShift) +
static_cast<result_type>(g() - kMin);
}
return r;
}

@ -44,7 +44,7 @@ class RandenPoolSeedSeq {
// Generate random unsigned values directly into the buffer.
template <typename Contiguous>
void generate_impl(ContiguousTag, Contiguous begin, Contiguous end) {
const size_t n = std::distance(begin, end);
const size_t n = static_cast<size_t>(std::distance(begin, end));
auto* a = &(*begin);
RandenPool<uint8_t>::Fill(
absl::MakeSpan(reinterpret_cast<uint8_t*>(a), sizeof(*a) * n));

@ -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<int>(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<int>(bit_width(mantissa));
const int shift = NormalizedShiftSize<FloatType>(mantissa_width, exponent);
bool result_exact;
exponent += shift;

@ -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<char>(size << 1);
tag() = static_cast<int8_t>(size << 1);
}
// Compares 'this' inlined data with rhs. The comparison is a straightforward

Loading…
Cancel
Save