Fix "unsafe narrowing" warnings in absl, 7/n.

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 focuses on .cc files in debugging/internal/.)

Bug: chromium:1292951
PiperOrigin-RevId: 470812243
Change-Id: I5578030bb42ba73cb83d4df84f89e431ceac8992
pull/1267/head
Abseil Team 2 years ago committed by Copybara-Service
parent 75691f1c32
commit d9382f7290
  1. 2
      absl/debugging/internal/address_is_readable.cc
  2. 12
      absl/debugging/internal/demangle.cc
  3. 2
      absl/debugging/internal/demangle.h
  4. 25
      absl/debugging/internal/elf_mem_image.cc
  5. 2
      absl/debugging/internal/stacktrace_generic-inl.inc
  6. 12
      absl/debugging/internal/stacktrace_x86-inl.inc
  7. 5
      absl/debugging/internal/vdso_support.cc

@ -52,7 +52,7 @@ namespace debugging_internal {
bool AddressIsReadable(const void *addr) { bool AddressIsReadable(const void *addr) {
// Align address on 8-byte boundary. On aarch64, checking last // Align address on 8-byte boundary. On aarch64, checking last
// byte before inaccessible page returned unexpected EFAULT. // byte before inaccessible page returned unexpected EFAULT.
const uintptr_t u_addr = reinterpret_cast<uintptr_t>(addr) & ~7; const uintptr_t u_addr = reinterpret_cast<uintptr_t>(addr) & ~uintptr_t{7};
addr = reinterpret_cast<const void *>(u_addr); addr = reinterpret_cast<const void *>(u_addr);
// rt_sigprocmask below will succeed for this input. // rt_sigprocmask below will succeed for this input.

@ -253,11 +253,13 @@ static bool StrPrefix(const char *str, const char *prefix) {
return prefix[i] == '\0'; // Consumed everything in "prefix". return prefix[i] == '\0'; // Consumed everything in "prefix".
} }
static void InitState(State *state, const char *mangled, char *out, static void InitState(State* state,
int out_size) { const char* mangled,
char* out,
size_t out_size) {
state->mangled_begin = mangled; state->mangled_begin = mangled;
state->out = out; state->out = out;
state->out_end_idx = out_size; state->out_end_idx = static_cast<int>(out_size);
state->recursion_depth = 0; state->recursion_depth = 0;
state->steps = 0; state->steps = 0;
@ -451,7 +453,7 @@ static bool MaybeAppendDecimal(State *state, int val) {
// one-past-the-end and manipulate one character before the pointer. // one-past-the-end and manipulate one character before the pointer.
char *p = &buf[kMaxLength]; char *p = &buf[kMaxLength];
do { // val=0 is the only input that should write a leading zero digit. do { // val=0 is the only input that should write a leading zero digit.
*--p = (val % 10) + '0'; *--p = static_cast<char>((val % 10) + '0');
val /= 10; val /= 10;
} while (p > buf && val != 0); } while (p > buf && val != 0);
@ -1974,7 +1976,7 @@ static bool Overflowed(const State *state) {
} }
// The demangler entry point. // The demangler entry point.
bool Demangle(const char *mangled, char *out, int out_size) { bool Demangle(const char* mangled, char* out, size_t out_size) {
State state; State state;
InitState(&state, mangled, out, out_size); InitState(&state, mangled, out, out_size);
return ParseTopLevelMangledName(&state) && !Overflowed(&state) && return ParseTopLevelMangledName(&state) && !Overflowed(&state) &&

@ -62,7 +62,7 @@ namespace debugging_internal {
// Demangle `mangled`. On success, return true and write the // Demangle `mangled`. On success, return true and write the
// demangled symbol name to `out`. Otherwise, return false. // demangled symbol name to `out`. Otherwise, return false.
// `out` is modified even if demangling is unsuccessful. // `out` is modified even if demangling is unsuccessful.
bool Demangle(const char *mangled, char *out, int out_size); bool Demangle(const char* mangled, char* out, size_t out_size);
} // namespace debugging_internal } // namespace debugging_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END

@ -91,7 +91,7 @@ int ElfMemImage::GetNumSymbols() const {
return 0; return 0;
} }
// See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash // See http://www.caldera.com/developers/gabi/latest/ch5.dynamic.html#hash
return hash_[1]; return static_cast<int>(hash_[1]);
} }
const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const { const ElfW(Sym) *ElfMemImage::GetDynsym(int index) const {
@ -105,11 +105,9 @@ const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
} }
const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const { const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
ABSL_RAW_CHECK(index < ehdr_->e_phnum, "index out of range"); ABSL_RAW_CHECK(index >= 0 && index < ehdr_->e_phnum, "index out of range");
return GetTableElement<ElfW(Phdr)>(ehdr_, return GetTableElement<ElfW(Phdr)>(ehdr_, ehdr_->e_phoff, ehdr_->e_phentsize,
ehdr_->e_phoff, static_cast<size_t>(index));
ehdr_->e_phentsize,
index);
} }
const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const { const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
@ -159,7 +157,8 @@ void ElfMemImage::Init(const void *base) {
hash_ = nullptr; hash_ = nullptr;
strsize_ = 0; strsize_ = 0;
verdefnum_ = 0; verdefnum_ = 0;
link_base_ = ~0L; // Sentinel: PT_LOAD .p_vaddr can't possibly be this. // Sentinel: PT_LOAD .p_vaddr can't possibly be this.
link_base_ = ~ElfW(Addr){0}; // NOLINT(readability/braces)
if (!base) { if (!base) {
return; return;
} }
@ -218,11 +217,11 @@ void ElfMemImage::Init(const void *base) {
} }
ptrdiff_t relocation = ptrdiff_t relocation =
base_as_char - reinterpret_cast<const char *>(link_base_); base_as_char - reinterpret_cast<const char *>(link_base_);
ElfW(Dyn) *dynamic_entry = ElfW(Dyn)* dynamic_entry = reinterpret_cast<ElfW(Dyn)*>(
reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr + static_cast<intptr_t>(dynamic_program_header->p_vaddr) + relocation);
relocation);
for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) { for (; dynamic_entry->d_tag != DT_NULL; ++dynamic_entry) {
const auto value = dynamic_entry->d_un.d_val + relocation; const auto value =
static_cast<intptr_t>(dynamic_entry->d_un.d_val) + relocation;
switch (dynamic_entry->d_tag) { switch (dynamic_entry->d_tag) {
case DT_HASH: case DT_HASH:
hash_ = reinterpret_cast<ElfW(Word) *>(value); hash_ = reinterpret_cast<ElfW(Word) *>(value);
@ -240,10 +239,10 @@ void ElfMemImage::Init(const void *base) {
verdef_ = reinterpret_cast<ElfW(Verdef) *>(value); verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
break; break;
case DT_VERDEFNUM: case DT_VERDEFNUM:
verdefnum_ = dynamic_entry->d_un.d_val; verdefnum_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
break; break;
case DT_STRSZ: case DT_STRSZ:
strsize_ = dynamic_entry->d_un.d_val; strsize_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
break; break;
default: default:
// Unrecognized entries explicitly ignored. // Unrecognized entries explicitly ignored.

@ -80,7 +80,7 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
if (IS_STACK_FRAMES) { if (IS_STACK_FRAMES) {
// No implementation for finding out the stack frame sizes yet. // No implementation for finding out the stack frame sizes yet.
memset(sizes, 0, sizeof(*sizes) * result_count); memset(sizes, 0, sizeof(*sizes) * static_cast<size_t>(result_count));
} }
if (min_dropped_frames != nullptr) { if (min_dropped_frames != nullptr) {
if (size - skip_count - max_depth > 0) { if (size - skip_count - max_depth > 0) {

@ -140,13 +140,14 @@ static uintptr_t GetFP(const void *vuc) {
// TODO(bcmills): -momit-leaf-frame-pointer is currently the default // TODO(bcmills): -momit-leaf-frame-pointer is currently the default
// behavior when building with clang. Talk to the C++ toolchain team about // behavior when building with clang. Talk to the C++ toolchain team about
// fixing that. // fixing that.
if (bp >= sp && bp - sp <= kMaxFrameBytes) return bp; if (bp >= sp && bp - sp <= kMaxFrameBytes)
return static_cast<uintptr_t>(bp);
// If bp isn't a plausible frame pointer, return the stack pointer instead. // If bp isn't a plausible frame pointer, return the stack pointer instead.
// If we're lucky, it points to the start of a stack frame; otherwise, we'll // If we're lucky, it points to the start of a stack frame; otherwise, we'll
// get one frame of garbage in the stack trace and fail the sanity check on // get one frame of garbage in the stack trace and fail the sanity check on
// the next iteration. // the next iteration.
return sp; return static_cast<uintptr_t>(sp);
} }
#endif #endif
return 0; return 0;
@ -310,7 +311,8 @@ static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
int n = 0; int n = 0;
void **fp = reinterpret_cast<void **>(__builtin_frame_address(0)); void **fp = reinterpret_cast<void **>(__builtin_frame_address(0));
size_t stack_low = getpagesize(); // Assume that the first page is not stack. // Assume that the first page is not stack.
size_t stack_low = static_cast<size_t>(getpagesize());
size_t stack_high = std::numeric_limits<size_t>::max() - sizeof(void *); size_t stack_high = std::numeric_limits<size_t>::max() - sizeof(void *);
while (fp && n < max_depth) { while (fp && n < max_depth) {
@ -327,7 +329,9 @@ static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
result[n] = *(fp + 1); result[n] = *(fp + 1);
if (IS_STACK_FRAMES) { if (IS_STACK_FRAMES) {
if (next_fp > fp) { if (next_fp > fp) {
sizes[n] = (uintptr_t)next_fp - (uintptr_t)fp; sizes[n] = static_cast<int>(
reinterpret_cast<uintptr_t>(next_fp) -
reinterpret_cast<uintptr_t>(fp));
} else { } else {
// A frame-size of 0 is used to indicate unknown frame size. // A frame-size of 0 is used to indicate unknown frame size.
sizes[n] = 0; sizes[n] = 0;

@ -193,8 +193,9 @@ long VDSOSupport::InitAndGetCPU(unsigned *cpu, // NOLINT(runtime/int)
ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
int GetCPU() { int GetCPU() {
unsigned cpu; unsigned cpu;
int ret_code = (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr); long ret_code = // NOLINT(runtime/int)
return ret_code == 0 ? cpu : ret_code; (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr);
return ret_code == 0 ? static_cast<int>(cpu) : static_cast<int>(ret_code);
} }
} // namespace debugging_internal } // namespace debugging_internal

Loading…
Cancel
Save