summaryrefslogtreecommitdiff
path: root/absl/debugging/internal
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2022-08-29 14:14:51 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-08-29 14:15:52 -0700
commitd9382f72901dd2bd40d38834eb591cea784e90e5 (patch)
tree39086e30a602efa312fa3a4389ae41032c5be411 /absl/debugging/internal
parent75691f1c3292969c5a85288a3cee8ae831203302 (diff)
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
Diffstat (limited to 'absl/debugging/internal')
-rw-r--r--absl/debugging/internal/address_is_readable.cc2
-rw-r--r--absl/debugging/internal/demangle.cc12
-rw-r--r--absl/debugging/internal/demangle.h2
-rw-r--r--absl/debugging/internal/elf_mem_image.cc25
-rw-r--r--absl/debugging/internal/stacktrace_generic-inl.inc2
-rw-r--r--absl/debugging/internal/stacktrace_x86-inl.inc12
-rw-r--r--absl/debugging/internal/vdso_support.cc5
7 files changed, 33 insertions, 27 deletions
diff --git a/absl/debugging/internal/address_is_readable.cc b/absl/debugging/internal/address_is_readable.cc
index 4be6256b..91eaa76f 100644
--- a/absl/debugging/internal/address_is_readable.cc
+++ b/absl/debugging/internal/address_is_readable.cc
@@ -52,7 +52,7 @@ namespace debugging_internal {
bool AddressIsReadable(const void *addr) {
// Align address on 8-byte boundary. On aarch64, checking last
// 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);
// rt_sigprocmask below will succeed for this input.
diff --git a/absl/debugging/internal/demangle.cc b/absl/debugging/internal/demangle.cc
index 161de846..f2832915 100644
--- a/absl/debugging/internal/demangle.cc
+++ b/absl/debugging/internal/demangle.cc
@@ -253,11 +253,13 @@ static bool StrPrefix(const char *str, const char *prefix) {
return prefix[i] == '\0'; // Consumed everything in "prefix".
}
-static void InitState(State *state, const char *mangled, char *out,
- int out_size) {
+static void InitState(State* state,
+ const char* mangled,
+ char* out,
+ size_t out_size) {
state->mangled_begin = mangled;
state->out = out;
- state->out_end_idx = out_size;
+ state->out_end_idx = static_cast<int>(out_size);
state->recursion_depth = 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.
char *p = &buf[kMaxLength];
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;
} while (p > buf && val != 0);
@@ -1974,7 +1976,7 @@ static bool Overflowed(const State *state) {
}
// 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;
InitState(&state, mangled, out, out_size);
return ParseTopLevelMangledName(&state) && !Overflowed(&state) &&
diff --git a/absl/debugging/internal/demangle.h b/absl/debugging/internal/demangle.h
index c314d9bc..e1f15698 100644
--- a/absl/debugging/internal/demangle.h
+++ b/absl/debugging/internal/demangle.h
@@ -62,7 +62,7 @@ namespace debugging_internal {
// Demangle `mangled`. On success, return true and write the
// demangled symbol name to `out`. Otherwise, return false.
// `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
ABSL_NAMESPACE_END
diff --git a/absl/debugging/internal/elf_mem_image.cc b/absl/debugging/internal/elf_mem_image.cc
index a9d66714..42dcd3cd 100644
--- a/absl/debugging/internal/elf_mem_image.cc
+++ b/absl/debugging/internal/elf_mem_image.cc
@@ -91,7 +91,7 @@ int ElfMemImage::GetNumSymbols() const {
return 0;
}
// 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 {
@@ -105,11 +105,9 @@ const ElfW(Versym) *ElfMemImage::GetVersym(int index) const {
}
const ElfW(Phdr) *ElfMemImage::GetPhdr(int index) const {
- ABSL_RAW_CHECK(index < ehdr_->e_phnum, "index out of range");
- return GetTableElement<ElfW(Phdr)>(ehdr_,
- ehdr_->e_phoff,
- ehdr_->e_phentsize,
- index);
+ ABSL_RAW_CHECK(index >= 0 && index < ehdr_->e_phnum, "index out of range");
+ return GetTableElement<ElfW(Phdr)>(ehdr_, ehdr_->e_phoff, ehdr_->e_phentsize,
+ static_cast<size_t>(index));
}
const char *ElfMemImage::GetDynstr(ElfW(Word) offset) const {
@@ -159,7 +157,8 @@ void ElfMemImage::Init(const void *base) {
hash_ = nullptr;
strsize_ = 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) {
return;
}
@@ -218,11 +217,11 @@ void ElfMemImage::Init(const void *base) {
}
ptrdiff_t relocation =
base_as_char - reinterpret_cast<const char *>(link_base_);
- ElfW(Dyn) *dynamic_entry =
- reinterpret_cast<ElfW(Dyn) *>(dynamic_program_header->p_vaddr +
- relocation);
+ ElfW(Dyn)* dynamic_entry = reinterpret_cast<ElfW(Dyn)*>(
+ static_cast<intptr_t>(dynamic_program_header->p_vaddr) + relocation);
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) {
case DT_HASH:
hash_ = reinterpret_cast<ElfW(Word) *>(value);
@@ -240,10 +239,10 @@ void ElfMemImage::Init(const void *base) {
verdef_ = reinterpret_cast<ElfW(Verdef) *>(value);
break;
case DT_VERDEFNUM:
- verdefnum_ = dynamic_entry->d_un.d_val;
+ verdefnum_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
break;
case DT_STRSZ:
- strsize_ = dynamic_entry->d_un.d_val;
+ strsize_ = static_cast<size_t>(dynamic_entry->d_un.d_val);
break;
default:
// Unrecognized entries explicitly ignored.
diff --git a/absl/debugging/internal/stacktrace_generic-inl.inc b/absl/debugging/internal/stacktrace_generic-inl.inc
index b2792a1f..5fa169a7 100644
--- a/absl/debugging/internal/stacktrace_generic-inl.inc
+++ b/absl/debugging/internal/stacktrace_generic-inl.inc
@@ -80,7 +80,7 @@ static int UnwindImpl(void** result, int* sizes, int max_depth, int skip_count,
if (IS_STACK_FRAMES) {
// 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 (size - skip_count - max_depth > 0) {
diff --git a/absl/debugging/internal/stacktrace_x86-inl.inc b/absl/debugging/internal/stacktrace_x86-inl.inc
index 1b5d8235..1354cb37 100644
--- a/absl/debugging/internal/stacktrace_x86-inl.inc
+++ b/absl/debugging/internal/stacktrace_x86-inl.inc
@@ -140,13 +140,14 @@ static uintptr_t GetFP(const void *vuc) {
// TODO(bcmills): -momit-leaf-frame-pointer is currently the default
// behavior when building with clang. Talk to the C++ toolchain team about
// 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 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
// the next iteration.
- return sp;
+ return static_cast<uintptr_t>(sp);
}
#endif
return 0;
@@ -310,7 +311,8 @@ static int UnwindImpl(void **result, int *sizes, int max_depth, int skip_count,
int n = 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 *);
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);
if (IS_STACK_FRAMES) {
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 {
// A frame-size of 0 is used to indicate unknown frame size.
sizes[n] = 0;
diff --git a/absl/debugging/internal/vdso_support.cc b/absl/debugging/internal/vdso_support.cc
index 40eb055f..8a588eaf 100644
--- a/absl/debugging/internal/vdso_support.cc
+++ b/absl/debugging/internal/vdso_support.cc
@@ -193,8 +193,9 @@ long VDSOSupport::InitAndGetCPU(unsigned *cpu, // NOLINT(runtime/int)
ABSL_ATTRIBUTE_NO_SANITIZE_MEMORY
int GetCPU() {
unsigned cpu;
- int ret_code = (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr);
- return ret_code == 0 ? cpu : ret_code;
+ long ret_code = // NOLINT(runtime/int)
+ (*VDSOSupport::getcpu_fn_)(&cpu, nullptr, nullptr);
+ return ret_code == 0 ? static_cast<int>(cpu) : static_cast<int>(ret_code);
}
} // namespace debugging_internal