summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-10-13 14:12:39 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-10-13 14:13:57 -0700
commitf073fe8ee5dcb0aa18c893198747062f2f51ab59 (patch)
treefa473e3a4a77146484d19b456371452418e4a297
parentcbaba1a9bfdc8900e0d360e87d2fee137f6f7d9e (diff)
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
-rw-r--r--absl/copts/GENERATED_AbseilCopts.cmake10
-rw-r--r--absl/copts/GENERATED_copts.bzl10
-rw-r--r--absl/copts/copts.py7
-rw-r--r--absl/debugging/failure_signal_handler.cc3
-rw-r--r--absl/debugging/internal/stacktrace_aarch64-inl.inc16
-rw-r--r--absl/numeric/bits_benchmark.cc12
-rw-r--r--absl/random/internal/fast_uniform_bits.h3
-rw-r--r--absl/random/internal/nonsecure_base.h2
-rw-r--r--absl/strings/charconv.cc8
-rw-r--r--absl/strings/internal/cord_internal.h2
10 files changed, 45 insertions, 28 deletions
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<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.
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<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++;
}
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 <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);
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<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;
}
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 <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));
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<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;
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<char>(size << 1);
+ tag() = static_cast<int8_t>(size << 1);
}
// Compares 'this' inlined data with rhs. The comparison is a straightforward