Commit 69199fce by Abseil Team Committed by Copybara-Service

Fix "unsafe narrowing" warnings in absl, 9/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 miscellaneous non-test source files.)

Bug: chromium:1292951
PiperOrigin-RevId: 473054605
Change-Id: Ifd7b24966613ca915511a3a607095508068200b8
parent 518984e4
...@@ -626,9 +626,9 @@ class InlinedVector { ...@@ -626,9 +626,9 @@ class InlinedVector {
ABSL_HARDENING_ASSERT(pos <= end()); ABSL_HARDENING_ASSERT(pos <= end());
if (ABSL_PREDICT_TRUE(first != last)) { if (ABSL_PREDICT_TRUE(first != last)) {
return storage_.Insert(pos, return storage_.Insert(
IteratorValueAdapter<A, ForwardIterator>(first), pos, IteratorValueAdapter<A, ForwardIterator>(first),
std::distance(first, last)); static_cast<size_type>(std::distance(first, last)));
} else { } else {
return const_cast<iterator>(pos); return const_cast<iterator>(pos);
} }
...@@ -645,7 +645,7 @@ class InlinedVector { ...@@ -645,7 +645,7 @@ class InlinedVector {
ABSL_HARDENING_ASSERT(pos >= begin()); ABSL_HARDENING_ASSERT(pos >= begin());
ABSL_HARDENING_ASSERT(pos <= end()); ABSL_HARDENING_ASSERT(pos <= end());
size_type index = std::distance(cbegin(), pos); size_type index = static_cast<size_type>(std::distance(cbegin(), pos));
for (size_type i = index; first != last; ++i, static_cast<void>(++first)) { for (size_type i = index; first != last; ++i, static_cast<void>(++first)) {
insert(data() + i, *first); insert(data() + i, *first);
} }
......
...@@ -135,7 +135,7 @@ static bool SetupAlternateStackOnce() { ...@@ -135,7 +135,7 @@ static bool SetupAlternateStackOnce() {
#if defined(__wasm__) || defined (__asjms__) #if defined(__wasm__) || defined (__asjms__)
const size_t page_mask = getpagesize() - 1; const size_t page_mask = getpagesize() - 1;
#else #else
const size_t page_mask = sysconf(_SC_PAGESIZE) - 1; const size_t page_mask = static_cast<size_t>(sysconf(_SC_PAGESIZE)) - 1;
#endif #endif
size_t stack_size = size_t stack_size =
(std::max<size_t>(SIGSTKSZ, 65536) + page_mask) & ~page_mask; (std::max<size_t>(SIGSTKSZ, 65536) + page_mask) & ~page_mask;
...@@ -356,7 +356,7 @@ static void AbslFailureSignalHandler(int signo, siginfo_t*, void* ucontext) { ...@@ -356,7 +356,7 @@ static void AbslFailureSignalHandler(int signo, siginfo_t*, void* ucontext) {
if (fsh_options.alarm_on_failure_secs > 0) { if (fsh_options.alarm_on_failure_secs > 0) {
alarm(0); // Cancel any existing alarms. alarm(0); // Cancel any existing alarms.
signal(SIGALRM, ImmediateAbortSignalHandler); signal(SIGALRM, ImmediateAbortSignalHandler);
alarm(fsh_options.alarm_on_failure_secs); alarm(static_cast<unsigned int>(fsh_options.alarm_on_failure_secs));
} }
#endif #endif
......
...@@ -83,13 +83,14 @@ bool Symbolize(const void* pc, char* out, int out_size) { ...@@ -83,13 +83,14 @@ bool Symbolize(const void* pc, char* out, int out_size) {
memmove(out, tmp_buf, len + 1); memmove(out, tmp_buf, len + 1);
} }
} else { } else {
strncpy(out, symbol.c_str(), out_size); strncpy(out, symbol.c_str(), static_cast<size_t>(out_size));
} }
if (out[out_size - 1] != '\0') { if (out[out_size - 1] != '\0') {
// strncpy() does not '\0' terminate when it truncates. // strncpy() does not '\0' terminate when it truncates.
static constexpr char kEllipsis[] = "..."; static constexpr char kEllipsis[] = "...";
int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1); size_t ellipsis_size =
std::min(sizeof(kEllipsis) - 1, static_cast<size_t>(out_size) - 1);
memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
out[out_size - 1] = '\0'; out[out_size - 1] = '\0';
} }
......
...@@ -252,21 +252,21 @@ class AddrMap { ...@@ -252,21 +252,21 @@ class AddrMap {
public: public:
AddrMap() : size_(0), allocated_(0), obj_(nullptr) {} AddrMap() : size_(0), allocated_(0), obj_(nullptr) {}
~AddrMap() { base_internal::LowLevelAlloc::Free(obj_); } ~AddrMap() { base_internal::LowLevelAlloc::Free(obj_); }
int Size() const { return size_; } size_t Size() const { return size_; }
ObjFile *At(int i) { return &obj_[i]; } ObjFile *At(size_t i) { return &obj_[i]; }
ObjFile *Add(); ObjFile *Add();
void Clear(); void Clear();
private: private:
int size_; // count of valid elements (<= allocated_) size_t size_; // count of valid elements (<= allocated_)
int allocated_; // count of allocated elements size_t allocated_; // count of allocated elements
ObjFile *obj_; // array of allocated_ elements ObjFile *obj_; // array of allocated_ elements
AddrMap(const AddrMap &) = delete; AddrMap(const AddrMap &) = delete;
AddrMap &operator=(const AddrMap &) = delete; AddrMap &operator=(const AddrMap &) = delete;
}; };
void AddrMap::Clear() { void AddrMap::Clear() {
for (int i = 0; i != size_; i++) { for (size_t i = 0; i != size_; i++) {
At(i)->~ObjFile(); At(i)->~ObjFile();
} }
size_ = 0; size_ = 0;
...@@ -274,7 +274,7 @@ void AddrMap::Clear() { ...@@ -274,7 +274,7 @@ void AddrMap::Clear() {
ObjFile *AddrMap::Add() { ObjFile *AddrMap::Add() {
if (size_ == allocated_) { if (size_ == allocated_) {
int new_allocated = allocated_ * 2 + 50; size_t new_allocated = allocated_ * 2 + 50;
ObjFile *new_obj_ = ObjFile *new_obj_ =
static_cast<ObjFile *>(base_internal::LowLevelAlloc::AllocWithArena( static_cast<ObjFile *>(base_internal::LowLevelAlloc::AllocWithArena(
new_allocated * sizeof(*new_obj_), SigSafeArena())); new_allocated * sizeof(*new_obj_), SigSafeArena()));
...@@ -300,7 +300,7 @@ class Symbolizer { ...@@ -300,7 +300,7 @@ class Symbolizer {
private: private:
char *CopyString(const char *s) { char *CopyString(const char *s) {
int len = strlen(s); size_t len = strlen(s);
char *dst = static_cast<char *>( char *dst = static_cast<char *>(
base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena()));
ABSL_RAW_CHECK(dst != nullptr, "out of memory"); ABSL_RAW_CHECK(dst != nullptr, "out of memory");
...@@ -321,8 +321,8 @@ class Symbolizer { ...@@ -321,8 +321,8 @@ class Symbolizer {
FindSymbolResult GetSymbolFromObjectFile(const ObjFile &obj, FindSymbolResult GetSymbolFromObjectFile(const ObjFile &obj,
const void *const pc, const void *const pc,
const ptrdiff_t relocation, const ptrdiff_t relocation,
char *out, int out_size, char *out, size_t out_size,
char *tmp_buf, int tmp_buf_size); char *tmp_buf, size_t tmp_buf_size);
const char *GetUncachedSymbol(const void *pc); const char *GetUncachedSymbol(const void *pc);
enum { enum {
...@@ -353,11 +353,11 @@ static std::atomic<Symbolizer *> g_cached_symbolizer; ...@@ -353,11 +353,11 @@ static std::atomic<Symbolizer *> g_cached_symbolizer;
} // namespace } // namespace
static int SymbolizerSize() { static size_t SymbolizerSize() {
#if defined(__wasm__) || defined(__asmjs__) #if defined(__wasm__) || defined(__asmjs__)
int pagesize = getpagesize(); auto pagesize = static_cast<size_t>(getpagesize());
#else #else
int pagesize = sysconf(_SC_PAGESIZE); auto pagesize = static_cast<size_t>(sysconf(_SC_PAGESIZE));
#endif #endif
return ((sizeof(Symbolizer) - 1) / pagesize + 1) * pagesize; return ((sizeof(Symbolizer) - 1) / pagesize + 1) * pagesize;
} }
...@@ -429,7 +429,7 @@ static ssize_t ReadPersistent(int fd, void *buf, size_t count) { ...@@ -429,7 +429,7 @@ static ssize_t ReadPersistent(int fd, void *buf, size_t count) {
if (len == 0) { // Reached EOF. if (len == 0) { // Reached EOF.
break; break;
} }
num_bytes += len; num_bytes += static_cast<size_t>(len);
} }
SAFE_ASSERT(num_bytes <= count); SAFE_ASSERT(num_bytes <= count);
return static_cast<ssize_t>(num_bytes); return static_cast<ssize_t>(num_bytes);
...@@ -478,36 +478,37 @@ static int FileGetElfType(const int fd) { ...@@ -478,36 +478,37 @@ static int FileGetElfType(const int fd) {
// inlined. // inlined.
static ABSL_ATTRIBUTE_NOINLINE bool GetSectionHeaderByType( static ABSL_ATTRIBUTE_NOINLINE bool GetSectionHeaderByType(
const int fd, ElfW(Half) sh_num, const off_t sh_offset, ElfW(Word) type, const int fd, ElfW(Half) sh_num, const off_t sh_offset, ElfW(Word) type,
ElfW(Shdr) * out, char *tmp_buf, int tmp_buf_size) { ElfW(Shdr) * out, char *tmp_buf, size_t tmp_buf_size) {
ElfW(Shdr) *buf = reinterpret_cast<ElfW(Shdr) *>(tmp_buf); ElfW(Shdr) *buf = reinterpret_cast<ElfW(Shdr) *>(tmp_buf);
const int buf_entries = tmp_buf_size / sizeof(buf[0]); const size_t buf_entries = tmp_buf_size / sizeof(buf[0]);
const int buf_bytes = buf_entries * sizeof(buf[0]); const size_t buf_bytes = buf_entries * sizeof(buf[0]);
for (int i = 0; i < sh_num;) { for (size_t i = 0; static_cast<int>(i) < sh_num;) {
const ssize_t num_bytes_left = (sh_num - i) * sizeof(buf[0]); const size_t num_bytes_left =
const ssize_t num_bytes_to_read = (static_cast<size_t>(sh_num) - i) * sizeof(buf[0]);
const size_t num_bytes_to_read =
(buf_bytes > num_bytes_left) ? num_bytes_left : buf_bytes; (buf_bytes > num_bytes_left) ? num_bytes_left : buf_bytes;
const off_t offset = sh_offset + i * sizeof(buf[0]); const off_t offset = sh_offset + static_cast<off_t>(i * sizeof(buf[0]));
const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read, offset); const ssize_t len = ReadFromOffset(fd, buf, num_bytes_to_read, offset);
if (len < 0) { if (len < 0) {
ABSL_RAW_LOG( ABSL_RAW_LOG(
WARNING, WARNING,
"Reading %zd bytes from offset %ju returned %zd which is negative.", "Reading %zu bytes from offset %ju returned %zd which is negative.",
num_bytes_to_read, static_cast<intmax_t>(offset), len); num_bytes_to_read, static_cast<intmax_t>(offset), len);
return false; return false;
} }
if (len % sizeof(buf[0]) != 0) { if (static_cast<size_t>(len) % sizeof(buf[0]) != 0) {
ABSL_RAW_LOG( ABSL_RAW_LOG(
WARNING, WARNING,
"Reading %zd bytes from offset %jd returned %zd which is not a " "Reading %zu bytes from offset %jd returned %zd which is not a "
"multiple of %zu.", "multiple of %zu.",
num_bytes_to_read, static_cast<intmax_t>(offset), len, num_bytes_to_read, static_cast<intmax_t>(offset), len,
sizeof(buf[0])); sizeof(buf[0]));
return false; return false;
} }
const ssize_t num_headers_in_buf = len / sizeof(buf[0]); const size_t num_headers_in_buf = static_cast<size_t>(len) / sizeof(buf[0]);
SAFE_ASSERT(num_headers_in_buf <= buf_entries); SAFE_ASSERT(num_headers_in_buf <= buf_entries);
for (int j = 0; j < num_headers_in_buf; ++j) { for (size_t j = 0; j < num_headers_in_buf; ++j) {
if (buf[j].sh_type == type) { if (buf[j].sh_type == type) {
*out = buf[j]; *out = buf[j];
return true; return true;
...@@ -531,8 +532,8 @@ bool ForEachSection(int fd, ...@@ -531,8 +532,8 @@ bool ForEachSection(int fd,
} }
ElfW(Shdr) shstrtab; ElfW(Shdr) shstrtab;
off_t shstrtab_offset = off_t shstrtab_offset = static_cast<off_t>(elf_header.e_shoff) +
(elf_header.e_shoff + elf_header.e_shentsize * elf_header.e_shstrndx); elf_header.e_shentsize * elf_header.e_shstrndx;
if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) { if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
return false; return false;
} }
...@@ -540,22 +541,23 @@ bool ForEachSection(int fd, ...@@ -540,22 +541,23 @@ bool ForEachSection(int fd,
for (int i = 0; i < elf_header.e_shnum; ++i) { for (int i = 0; i < elf_header.e_shnum; ++i) {
ElfW(Shdr) out; ElfW(Shdr) out;
off_t section_header_offset = off_t section_header_offset =
(elf_header.e_shoff + elf_header.e_shentsize * i); static_cast<off_t>(elf_header.e_shoff) + elf_header.e_shentsize * i;
if (!ReadFromOffsetExact(fd, &out, sizeof(out), section_header_offset)) { if (!ReadFromOffsetExact(fd, &out, sizeof(out), section_header_offset)) {
return false; return false;
} }
off_t name_offset = shstrtab.sh_offset + out.sh_name; off_t name_offset = static_cast<off_t>(shstrtab.sh_offset) + out.sh_name;
char header_name[kMaxSectionNameLen]; char header_name[kMaxSectionNameLen];
ssize_t n_read = ssize_t n_read =
ReadFromOffset(fd, &header_name, kMaxSectionNameLen, name_offset); ReadFromOffset(fd, &header_name, kMaxSectionNameLen, name_offset);
if (n_read == -1) { if (n_read < 0) {
return false; return false;
} else if (n_read > kMaxSectionNameLen) { } else if (n_read > kMaxSectionNameLen) {
// Long read? // Long read?
return false; return false;
} }
absl::string_view name(header_name, strnlen(header_name, n_read)); absl::string_view name(header_name,
strnlen(header_name, static_cast<size_t>(n_read)));
if (!callback(name, out)) { if (!callback(name, out)) {
break; break;
} }
...@@ -582,19 +584,19 @@ bool GetSectionHeaderByName(int fd, const char *name, size_t name_len, ...@@ -582,19 +584,19 @@ bool GetSectionHeaderByName(int fd, const char *name, size_t name_len,
} }
ElfW(Shdr) shstrtab; ElfW(Shdr) shstrtab;
off_t shstrtab_offset = off_t shstrtab_offset = static_cast<off_t>(elf_header.e_shoff) +
(elf_header.e_shoff + elf_header.e_shentsize * elf_header.e_shstrndx); elf_header.e_shentsize * elf_header.e_shstrndx;
if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) { if (!ReadFromOffsetExact(fd, &shstrtab, sizeof(shstrtab), shstrtab_offset)) {
return false; return false;
} }
for (int i = 0; i < elf_header.e_shnum; ++i) { for (int i = 0; i < elf_header.e_shnum; ++i) {
off_t section_header_offset = off_t section_header_offset =
(elf_header.e_shoff + elf_header.e_shentsize * i); static_cast<off_t>(elf_header.e_shoff) + elf_header.e_shentsize * i;
if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) { if (!ReadFromOffsetExact(fd, out, sizeof(*out), section_header_offset)) {
return false; return false;
} }
off_t name_offset = shstrtab.sh_offset + out->sh_name; off_t name_offset = static_cast<off_t>(shstrtab.sh_offset) + out->sh_name;
ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset); ssize_t n_read = ReadFromOffset(fd, &header_name, name_len, name_offset);
if (n_read < 0) { if (n_read < 0) {
return false; return false;
...@@ -652,10 +654,10 @@ static bool InSection(const void *address, const ElfW(Shdr) * section) { ...@@ -652,10 +654,10 @@ static bool InSection(const void *address, const ElfW(Shdr) * section) {
} }
static const char *ComputeOffset(const char *base, ptrdiff_t offset) { static const char *ComputeOffset(const char *base, ptrdiff_t offset) {
// Note: cast to uintptr_t to avoid undefined behavior when base evaluates to // Note: cast to intptr_t to avoid undefined behavior when base evaluates to
// zero and offset is non-zero. // zero and offset is non-zero.
return reinterpret_cast<const char *>( return reinterpret_cast<const char *>(reinterpret_cast<intptr_t>(base) +
reinterpret_cast<uintptr_t>(base) + offset); offset);
} }
// Read a symbol table and look for the symbol containing the // Read a symbol table and look for the symbol containing the
...@@ -668,18 +670,18 @@ static const char *ComputeOffset(const char *base, ptrdiff_t offset) { ...@@ -668,18 +670,18 @@ static const char *ComputeOffset(const char *base, ptrdiff_t offset) {
// To keep stack consumption low, we would like this function to not get // To keep stack consumption low, we would like this function to not get
// inlined. // inlined.
static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
const void *const pc, const int fd, char *out, int out_size, const void *const pc, const int fd, char *out, size_t out_size,
ptrdiff_t relocation, const ElfW(Shdr) * strtab, const ElfW(Shdr) * symtab, ptrdiff_t relocation, const ElfW(Shdr) * strtab, const ElfW(Shdr) * symtab,
const ElfW(Shdr) * opd, char *tmp_buf, int tmp_buf_size) { const ElfW(Shdr) * opd, char *tmp_buf, size_t tmp_buf_size) {
if (symtab == nullptr) { if (symtab == nullptr) {
return SYMBOL_NOT_FOUND; return SYMBOL_NOT_FOUND;
} }
// Read multiple symbols at once to save read() calls. // Read multiple symbols at once to save read() calls.
ElfW(Sym) *buf = reinterpret_cast<ElfW(Sym) *>(tmp_buf); ElfW(Sym) *buf = reinterpret_cast<ElfW(Sym) *>(tmp_buf);
const int buf_entries = tmp_buf_size / sizeof(buf[0]); const size_t buf_entries = tmp_buf_size / sizeof(buf[0]);
const int num_symbols = symtab->sh_size / symtab->sh_entsize; const size_t num_symbols = symtab->sh_size / symtab->sh_entsize;
// On platforms using an .opd section (PowerPC & IA64), a function symbol // On platforms using an .opd section (PowerPC & IA64), a function symbol
// has the address of a function descriptor, which contains the real // has the address of a function descriptor, which contains the real
...@@ -694,17 +696,19 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -694,17 +696,19 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
ElfW(Sym) best_match; ElfW(Sym) best_match;
SafeMemZero(&best_match, sizeof(best_match)); SafeMemZero(&best_match, sizeof(best_match));
bool found_match = false; bool found_match = false;
for (int i = 0; i < num_symbols;) { for (size_t i = 0; i < num_symbols;) {
off_t offset = symtab->sh_offset + i * symtab->sh_entsize; off_t offset =
const int num_remaining_symbols = num_symbols - i; static_cast<off_t>(symtab->sh_offset + i * symtab->sh_entsize);
const int entries_in_chunk = std::min(num_remaining_symbols, buf_entries); const size_t num_remaining_symbols = num_symbols - i;
const int bytes_in_chunk = entries_in_chunk * sizeof(buf[0]); const size_t entries_in_chunk =
std::min(num_remaining_symbols, buf_entries);
const size_t bytes_in_chunk = entries_in_chunk * sizeof(buf[0]);
const ssize_t len = ReadFromOffset(fd, buf, bytes_in_chunk, offset); const ssize_t len = ReadFromOffset(fd, buf, bytes_in_chunk, offset);
SAFE_ASSERT(len >= 0); SAFE_ASSERT(len >= 0);
SAFE_ASSERT(len % sizeof(buf[0]) == 0); SAFE_ASSERT(static_cast<size_t>(len) % sizeof(buf[0]) == 0);
const ssize_t num_symbols_in_buf = len / sizeof(buf[0]); const size_t num_symbols_in_buf = static_cast<size_t>(len) / sizeof(buf[0]);
SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk); SAFE_ASSERT(num_symbols_in_buf <= entries_in_chunk);
for (int j = 0; j < num_symbols_in_buf; ++j) { for (size_t j = 0; j < num_symbols_in_buf; ++j) {
const ElfW(Sym) &symbol = buf[j]; const ElfW(Sym) &symbol = buf[j];
// For a DSO, a symbol address is relocated by the loading address. // For a DSO, a symbol address is relocated by the loading address.
...@@ -721,7 +725,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -721,7 +725,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// about what encoding is being used; we just want the real start address // about what encoding is being used; we just want the real start address
// of the function. // of the function.
start_address = reinterpret_cast<const char *>( start_address = reinterpret_cast<const char *>(
reinterpret_cast<uintptr_t>(start_address) & ~1); reinterpret_cast<uintptr_t>(start_address) & ~1u);
#endif #endif
if (deref_function_descriptor_pointer && if (deref_function_descriptor_pointer &&
...@@ -734,7 +738,8 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -734,7 +738,8 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// If pc is inside the .opd section, it points to a function descriptor. // If pc is inside the .opd section, it points to a function descriptor.
const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size; const size_t size = pc_in_opd ? kFunctionDescriptorSize : symbol.st_size;
const void *const end_address = ComputeOffset(start_address, size); const void *const end_address =
ComputeOffset(start_address, static_cast<ptrdiff_t>(size));
if (symbol.st_value != 0 && // Skip null value symbols. if (symbol.st_value != 0 && // Skip null value symbols.
symbol.st_shndx != 0 && // Skip undefined symbols. symbol.st_shndx != 0 && // Skip undefined symbols.
#ifdef STT_TLS #ifdef STT_TLS
...@@ -752,16 +757,19 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -752,16 +757,19 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
} }
if (found_match) { if (found_match) {
const size_t off = strtab->sh_offset + best_match.st_name; const off_t off =
static_cast<off_t>(strtab->sh_offset) + best_match.st_name;
const ssize_t n_read = ReadFromOffset(fd, out, out_size, off); const ssize_t n_read = ReadFromOffset(fd, out, out_size, off);
if (n_read <= 0) { if (n_read <= 0) {
// This should never happen. // This should never happen.
ABSL_RAW_LOG(WARNING, ABSL_RAW_LOG(
"Unable to read from fd %d at offset %zu: n_read = %zd", fd, WARNING,
off, n_read); "Unable to read from fd %d at offset %" PRId64 ": n_read = %zd", fd,
off, n_read);
return SYMBOL_NOT_FOUND; return SYMBOL_NOT_FOUND;
} }
ABSL_RAW_CHECK(n_read <= out_size, "ReadFromOffset read too much data."); ABSL_RAW_CHECK(static_cast<size_t>(n_read) <= out_size,
"ReadFromOffset read too much data.");
// strtab->sh_offset points into .strtab-like section that contains // strtab->sh_offset points into .strtab-like section that contains
// NUL-terminated strings: '\0foo\0barbaz\0...". // NUL-terminated strings: '\0foo\0barbaz\0...".
...@@ -769,7 +777,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -769,7 +777,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// sh_offset+st_name points to the start of symbol name, but we don't know // sh_offset+st_name points to the start of symbol name, but we don't know
// how long the symbol is, so we try to read as much as we have space for, // how long the symbol is, so we try to read as much as we have space for,
// and usually over-read (i.e. there is a NUL somewhere before n_read). // and usually over-read (i.e. there is a NUL somewhere before n_read).
if (memchr(out, '\0', n_read) == nullptr) { if (memchr(out, '\0', static_cast<size_t>(n_read)) == nullptr) {
// Either out_size was too small (n_read == out_size and no NUL), or // Either out_size was too small (n_read == out_size and no NUL), or
// we tried to read past the EOF (n_read < out_size) and .strtab is // we tried to read past the EOF (n_read < out_size) and .strtab is
// corrupt (missing terminating NUL; should never happen for valid ELF). // corrupt (missing terminating NUL; should never happen for valid ELF).
...@@ -787,7 +795,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol( ...@@ -787,7 +795,7 @@ static ABSL_ATTRIBUTE_NOINLINE FindSymbolResult FindSymbol(
// See FindSymbol() comment for description of return value. // See FindSymbol() comment for description of return value.
FindSymbolResult Symbolizer::GetSymbolFromObjectFile( FindSymbolResult Symbolizer::GetSymbolFromObjectFile(
const ObjFile &obj, const void *const pc, const ptrdiff_t relocation, const ObjFile &obj, const void *const pc, const ptrdiff_t relocation,
char *out, int out_size, char *tmp_buf, int tmp_buf_size) { char *out, size_t out_size, char *tmp_buf, size_t tmp_buf_size) {
ElfW(Shdr) symtab; ElfW(Shdr) symtab;
ElfW(Shdr) strtab; ElfW(Shdr) strtab;
ElfW(Shdr) opd; ElfW(Shdr) opd;
...@@ -810,13 +818,15 @@ FindSymbolResult Symbolizer::GetSymbolFromObjectFile( ...@@ -810,13 +818,15 @@ FindSymbolResult Symbolizer::GetSymbolFromObjectFile(
// Consult a regular symbol table, then fall back to the dynamic symbol table. // Consult a regular symbol table, then fall back to the dynamic symbol table.
for (const auto symbol_table_type : {SHT_SYMTAB, SHT_DYNSYM}) { for (const auto symbol_table_type : {SHT_SYMTAB, SHT_DYNSYM}) {
if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum, if (!GetSectionHeaderByType(obj.fd, obj.elf_header.e_shnum,
obj.elf_header.e_shoff, symbol_table_type, static_cast<off_t>(obj.elf_header.e_shoff),
static_cast<ElfW(Word)>(symbol_table_type),
&symtab, tmp_buf, tmp_buf_size)) { &symtab, tmp_buf, tmp_buf_size)) {
continue; continue;
} }
if (!ReadFromOffsetExact( if (!ReadFromOffsetExact(
obj.fd, &strtab, sizeof(strtab), obj.fd, &strtab, sizeof(strtab),
obj.elf_header.e_shoff + symtab.sh_link * sizeof(symtab))) { static_cast<off_t>(obj.elf_header.e_shoff +
symtab.sh_link * sizeof(symtab)))) {
continue; continue;
} }
const FindSymbolResult rc = const FindSymbolResult rc =
...@@ -858,7 +868,7 @@ class FileDescriptor { ...@@ -858,7 +868,7 @@ class FileDescriptor {
// and snprintf(). // and snprintf().
class LineReader { class LineReader {
public: public:
explicit LineReader(int fd, char *buf, int buf_len) explicit LineReader(int fd, char *buf, size_t buf_len)
: fd_(fd), : fd_(fd),
buf_len_(buf_len), buf_len_(buf_len),
buf_(buf), buf_(buf),
...@@ -886,12 +896,12 @@ class LineReader { ...@@ -886,12 +896,12 @@ class LineReader {
bol_ = eol_ + 1; // Advance to the next line in the buffer. bol_ = eol_ + 1; // Advance to the next line in the buffer.
SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_". SAFE_ASSERT(bol_ <= eod_); // "bol_" can point to "eod_".
if (!HasCompleteLine()) { if (!HasCompleteLine()) {
const int incomplete_line_length = eod_ - bol_; const auto incomplete_line_length = static_cast<size_t>(eod_ - bol_);
// Move the trailing incomplete line to the beginning. // Move the trailing incomplete line to the beginning.
memmove(buf_, bol_, incomplete_line_length); memmove(buf_, bol_, incomplete_line_length);
// Read text from file and append it. // Read text from file and append it.
char *const append_pos = buf_ + incomplete_line_length; char *const append_pos = buf_ + incomplete_line_length;
const int capacity_left = buf_len_ - incomplete_line_length; const size_t capacity_left = buf_len_ - incomplete_line_length;
const ssize_t num_bytes = const ssize_t num_bytes =
ReadPersistent(fd_, append_pos, capacity_left); ReadPersistent(fd_, append_pos, capacity_left);
if (num_bytes <= 0) { // EOF or error. if (num_bytes <= 0) { // EOF or error.
...@@ -914,7 +924,8 @@ class LineReader { ...@@ -914,7 +924,8 @@ class LineReader {
private: private:
char *FindLineFeed() const { char *FindLineFeed() const {
return reinterpret_cast<char *>(memchr(bol_, '\n', eod_ - bol_)); return reinterpret_cast<char *>(
memchr(bol_, '\n', static_cast<size_t>(eod_ - bol_)));
} }
bool BufferIsEmpty() const { return buf_ == eod_; } bool BufferIsEmpty() const { return buf_ == eod_; }
...@@ -924,7 +935,7 @@ class LineReader { ...@@ -924,7 +935,7 @@ class LineReader {
} }
const int fd_; const int fd_;
const int buf_len_; const size_t buf_len_;
char *const buf_; char *const buf_;
char *bol_; char *bol_;
char *eol_; char *eol_;
...@@ -942,7 +953,8 @@ static const char *GetHex(const char *start, const char *end, ...@@ -942,7 +953,8 @@ static const char *GetHex(const char *start, const char *end,
int ch = *p; int ch = *p;
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') || if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F') ||
(ch >= 'a' && ch <= 'f')) { (ch >= 'a' && ch <= 'f')) {
hex = (hex << 4) | (ch < 'A' ? ch - '0' : (ch & 0xF) + 9); hex = (hex << 4) |
static_cast<uint64_t>(ch < 'A' ? ch - '0' : (ch & 0xF) + 9);
} else { // Encountered the first non-hex character. } else { // Encountered the first non-hex character.
break; break;
} }
...@@ -974,7 +986,7 @@ static bool ShouldUseMapping(const char *const flags) { ...@@ -974,7 +986,7 @@ static bool ShouldUseMapping(const char *const flags) {
static ABSL_ATTRIBUTE_NOINLINE bool ReadAddrMap( static ABSL_ATTRIBUTE_NOINLINE bool ReadAddrMap(
bool (*callback)(const char *filename, const void *const start_addr, bool (*callback)(const char *filename, const void *const start_addr,
const void *const end_addr, uint64_t offset, void *arg), const void *const end_addr, uint64_t offset, void *arg),
void *arg, void *tmp_buf, int tmp_buf_size) { void *arg, void *tmp_buf, size_t tmp_buf_size) {
// Use /proc/self/task/<pid>/maps instead of /proc/self/maps. The latter // Use /proc/self/task/<pid>/maps instead of /proc/self/maps. The latter
// requires kernel to stop all threads, and is significantly slower when there // requires kernel to stop all threads, and is significantly slower when there
// are 1000s of threads. // are 1000s of threads.
...@@ -1089,10 +1101,10 @@ ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) { ...@@ -1089,10 +1101,10 @@ ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) {
} }
} }
int lo = 0; size_t lo = 0;
int hi = addr_map_.Size(); size_t hi = addr_map_.Size();
while (lo < hi) { while (lo < hi) {
int mid = (lo + hi) / 2; size_t mid = (lo + hi) / 2;
if (addr < addr_map_.At(mid)->end_addr) { if (addr < addr_map_.At(mid)->end_addr) {
hi = mid; hi = mid;
} else { } else {
...@@ -1114,7 +1126,7 @@ ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) { ...@@ -1114,7 +1126,7 @@ ObjFile *Symbolizer::FindObjFile(const void *const addr, size_t len) {
} }
void Symbolizer::ClearAddrMap() { void Symbolizer::ClearAddrMap() {
for (int i = 0; i != addr_map_.Size(); i++) { for (size_t i = 0; i != addr_map_.Size(); i++) {
ObjFile *o = addr_map_.At(i); ObjFile *o = addr_map_.At(i);
base_internal::LowLevelAlloc::Free(o->filename); base_internal::LowLevelAlloc::Free(o->filename);
if (o->fd >= 0) { if (o->fd >= 0) {
...@@ -1134,7 +1146,7 @@ bool Symbolizer::RegisterObjFile(const char *filename, ...@@ -1134,7 +1146,7 @@ bool Symbolizer::RegisterObjFile(const char *filename,
// Files are supposed to be added in the increasing address order. Make // Files are supposed to be added in the increasing address order. Make
// sure that's the case. // sure that's the case.
int addr_map_size = impl->addr_map_.Size(); size_t addr_map_size = impl->addr_map_.Size();
if (addr_map_size != 0) { if (addr_map_size != 0) {
ObjFile *old = impl->addr_map_.At(addr_map_size - 1); ObjFile *old = impl->addr_map_.At(addr_map_size - 1);
if (old->end_addr > end_addr) { if (old->end_addr > end_addr) {
...@@ -1178,12 +1190,12 @@ bool Symbolizer::RegisterObjFile(const char *filename, ...@@ -1178,12 +1190,12 @@ bool Symbolizer::RegisterObjFile(const char *filename,
// where the input symbol is demangled in-place. // where the input symbol is demangled in-place.
// To keep stack consumption low, we would like this function to not // To keep stack consumption low, we would like this function to not
// get inlined. // get inlined.
static ABSL_ATTRIBUTE_NOINLINE void DemangleInplace(char *out, int out_size, static ABSL_ATTRIBUTE_NOINLINE void DemangleInplace(char *out, size_t out_size,
char *tmp_buf, char *tmp_buf,
int tmp_buf_size) { size_t tmp_buf_size) {
if (Demangle(out, tmp_buf, tmp_buf_size)) { if (Demangle(out, tmp_buf, tmp_buf_size)) {
// Demangling succeeded. Copy to out if the space allows. // Demangling succeeded. Copy to out if the space allows.
int len = strlen(tmp_buf); size_t len = strlen(tmp_buf);
if (len + 1 <= out_size) { // +1 for '\0'. if (len + 1 <= out_size) { // +1 for '\0'.
SAFE_ASSERT(len < tmp_buf_size); SAFE_ASSERT(len < tmp_buf_size);
memmove(out, tmp_buf, len + 1); memmove(out, tmp_buf, len + 1);
...@@ -1226,7 +1238,8 @@ const char *Symbolizer::InsertSymbolInCache(const void *const pc, ...@@ -1226,7 +1238,8 @@ const char *Symbolizer::InsertSymbolInCache(const void *const pc,
SymbolCacheLine *line = GetCacheLine(pc); SymbolCacheLine *line = GetCacheLine(pc);
uint32_t max_age = 0; uint32_t max_age = 0;
int oldest_index = -1; size_t oldest_index = 0;
bool found_oldest_index = false;
for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) { for (size_t i = 0; i < ABSL_ARRAYSIZE(line->pc); ++i) {
if (line->pc[i] == nullptr) { if (line->pc[i] == nullptr) {
AgeSymbols(line); AgeSymbols(line);
...@@ -1238,11 +1251,12 @@ const char *Symbolizer::InsertSymbolInCache(const void *const pc, ...@@ -1238,11 +1251,12 @@ const char *Symbolizer::InsertSymbolInCache(const void *const pc,
if (line->age[i] >= max_age) { if (line->age[i] >= max_age) {
max_age = line->age[i]; max_age = line->age[i];
oldest_index = i; oldest_index = i;
found_oldest_index = true;
} }
} }
AgeSymbols(line); AgeSymbols(line);
ABSL_RAW_CHECK(oldest_index >= 0, "Corrupt cache"); ABSL_RAW_CHECK(found_oldest_index, "Corrupt cache");
base_internal::LowLevelAlloc::Free(line->name[oldest_index]); base_internal::LowLevelAlloc::Free(line->name[oldest_index]);
line->pc[oldest_index] = pc; line->pc[oldest_index] = pc;
line->name[oldest_index] = CopyString(name); line->name[oldest_index] = CopyString(name);
...@@ -1311,7 +1325,7 @@ static bool MaybeInitializeObjFile(ObjFile *obj) { ...@@ -1311,7 +1325,7 @@ static bool MaybeInitializeObjFile(ObjFile *obj) {
} }
const int phnum = obj->elf_header.e_phnum; const int phnum = obj->elf_header.e_phnum;
const int phentsize = obj->elf_header.e_phentsize; const int phentsize = obj->elf_header.e_phentsize;
size_t phoff = obj->elf_header.e_phoff; auto phoff = static_cast<off_t>(obj->elf_header.e_phoff);
size_t num_executable_load_segments = 0; size_t num_executable_load_segments = 0;
for (int j = 0; j < phnum; j++) { for (int j = 0; j < phnum; j++) {
ElfW(Phdr) phdr; ElfW(Phdr) phdr;
...@@ -1362,7 +1376,7 @@ const char *Symbolizer::GetUncachedSymbol(const void *pc) { ...@@ -1362,7 +1376,7 @@ const char *Symbolizer::GetUncachedSymbol(const void *pc) {
// //
// For obj->offset > 0, adjust the relocation since a mapping at offset // For obj->offset > 0, adjust the relocation since a mapping at offset
// X in the file will have a start address of [true relocation]+X. // X in the file will have a start address of [true relocation]+X.
relocation = start_addr - obj->offset; relocation = static_cast<ptrdiff_t>(start_addr - obj->offset);
// Note: some binaries have multiple "rx" LOAD segments. We must // Note: some binaries have multiple "rx" LOAD segments. We must
// find the right one. // find the right one.
...@@ -1537,7 +1551,7 @@ bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset ...@@ -1537,7 +1551,7 @@ bool RegisterFileMappingHint(const void *start, const void *end, uint64_t offset
ret = false; ret = false;
} else { } else {
// TODO(ckennelly): Move this into a string copy routine. // TODO(ckennelly): Move this into a string copy routine.
int len = strlen(filename); size_t len = strlen(filename);
char *dst = static_cast<char *>( char *dst = static_cast<char *>(
base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena())); base_internal::LowLevelAlloc::AllocWithArena(len + 1, SigSafeArena()));
ABSL_RAW_CHECK(dst != nullptr, "out of memory"); ABSL_RAW_CHECK(dst != nullptr, "out of memory");
...@@ -1593,16 +1607,17 @@ bool Symbolize(const void *pc, char *out, int out_size) { ...@@ -1593,16 +1607,17 @@ bool Symbolize(const void *pc, char *out, int out_size) {
const char *name = s->GetSymbol(pc); const char *name = s->GetSymbol(pc);
bool ok = false; bool ok = false;
if (name != nullptr && out_size > 0) { if (name != nullptr && out_size > 0) {
strncpy(out, name, out_size); strncpy(out, name, static_cast<size_t>(out_size));
ok = true; ok = true;
if (out[out_size - 1] != '\0') { if (out[static_cast<size_t>(out_size) - 1] != '\0') {
// strncpy() does not '\0' terminate when it truncates. Do so, with // strncpy() does not '\0' terminate when it truncates. Do so, with
// trailing ellipsis. // trailing ellipsis.
static constexpr char kEllipsis[] = "..."; static constexpr char kEllipsis[] = "...";
int ellipsis_size = size_t ellipsis_size =
std::min(implicit_cast<int>(strlen(kEllipsis)), out_size - 1); std::min(strlen(kEllipsis), static_cast<size_t>(out_size) - 1);
memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); memcpy(out + static_cast<size_t>(out_size) - ellipsis_size - 1, kEllipsis,
out[out_size - 1] = '\0'; ellipsis_size);
out[static_cast<size_t>(out_size) - 1] = '\0';
} }
} }
debugging_internal::FreeSymbolizer(s); debugging_internal::FreeSymbolizer(s);
......
...@@ -159,14 +159,14 @@ class ArgsList { ...@@ -159,14 +159,14 @@ class ArgsList {
// Returns success status: true if parsing successful, false otherwise. // Returns success status: true if parsing successful, false otherwise.
bool ReadFromFlagfile(const std::string& flag_file_name); bool ReadFromFlagfile(const std::string& flag_file_name);
int Size() const { return args_.size() - next_arg_; } size_t Size() const { return args_.size() - next_arg_; }
int FrontIndex() const { return next_arg_; } size_t FrontIndex() const { return next_arg_; }
absl::string_view Front() const { return args_[next_arg_]; } absl::string_view Front() const { return args_[next_arg_]; }
void PopFront() { next_arg_++; } void PopFront() { next_arg_++; }
private: private:
std::vector<std::string> args_; std::vector<std::string> args_;
int next_arg_; size_t next_arg_;
}; };
bool ArgsList::ReadFromFlagfile(const std::string& flag_file_name) { bool ArgsList::ReadFromFlagfile(const std::string& flag_file_name) {
...@@ -626,7 +626,7 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[], ...@@ -626,7 +626,7 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
std::vector<char*> output_args; std::vector<char*> output_args;
std::vector<char*> positional_args; std::vector<char*> positional_args;
output_args.reserve(argc); output_args.reserve(static_cast<size_t>(argc));
// This is the list of undefined flags. The element of the list is the pair // This is the list of undefined flags. The element of the list is the pair
// consisting of boolean indicating if flag came from command line (vs from // consisting of boolean indicating if flag came from command line (vs from
...@@ -795,8 +795,8 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[], ...@@ -795,8 +795,8 @@ std::vector<char*> ParseCommandLineImpl(int argc, char* argv[],
// All the remaining arguments are positional. // All the remaining arguments are positional.
if (!input_args.empty()) { if (!input_args.empty()) {
for (int arg_index = input_args.back().FrontIndex(); arg_index < argc; for (size_t arg_index = input_args.back().FrontIndex();
++arg_index) { arg_index < static_cast<size_t>(argc); ++arg_index) {
output_args.push_back(argv[arg_index]); output_args.push_back(argv[arg_index]);
} }
} }
......
...@@ -1801,8 +1801,8 @@ static inline bool EvalConditionAnnotated(const Condition *cond, Mutex *mu, ...@@ -1801,8 +1801,8 @@ static inline bool EvalConditionAnnotated(const Condition *cond, Mutex *mu,
// operation tsan considers that we've already released the mutex. // operation tsan considers that we've already released the mutex.
bool res = false; bool res = false;
#ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE #ifdef ABSL_INTERNAL_HAVE_TSAN_INTERFACE
const int flags = read_lock ? __tsan_mutex_read_lock : 0; const uint32_t flags = read_lock ? __tsan_mutex_read_lock : 0;
const int tryflags = flags | (trylock ? __tsan_mutex_try_lock : 0); const uint32_t tryflags = flags | (trylock ? __tsan_mutex_try_lock : 0);
#endif #endif
if (locking) { if (locking) {
// For lock we pretend that we have finished the operation, // For lock we pretend that we have finished the operation,
......
...@@ -617,7 +617,7 @@ timespec ToTimespec(Duration d) { ...@@ -617,7 +617,7 @@ timespec ToTimespec(Duration d) {
rep_lo -= kTicksPerSecond; rep_lo -= kTicksPerSecond;
} }
} }
ts.tv_sec = rep_hi; ts.tv_sec = static_cast<decltype(ts.tv_sec)>(rep_hi);
if (ts.tv_sec == rep_hi) { // no time_t narrowing if (ts.tv_sec == rep_hi) { // no time_t narrowing
ts.tv_nsec = rep_lo / kTicksPerNanosecond; ts.tv_nsec = rep_lo / kTicksPerNanosecond;
return ts; return ts;
...@@ -691,7 +691,7 @@ namespace { ...@@ -691,7 +691,7 @@ namespace {
char* Format64(char* ep, int width, int64_t v) { char* Format64(char* ep, int width, int64_t v) {
do { do {
--width; --width;
*--ep = '0' + (v % 10); // contiguous digits *--ep = static_cast<char>('0' + (v % 10)); // contiguous digits
} while (v /= 10); } while (v /= 10);
while (--width >= 0) *--ep = '0'; // zero pad while (--width >= 0) *--ep = '0'; // zero pad
return ep; return ep;
......
...@@ -297,7 +297,7 @@ timespec ToTimespec(Time t) { ...@@ -297,7 +297,7 @@ timespec ToTimespec(Time t) {
timespec ts; timespec ts;
absl::Duration d = time_internal::ToUnixDuration(t); absl::Duration d = time_internal::ToUnixDuration(t);
if (!time_internal::IsInfiniteDuration(d)) { if (!time_internal::IsInfiniteDuration(d)) {
ts.tv_sec = time_internal::GetRepHi(d); ts.tv_sec = static_cast<decltype(ts.tv_sec)>(time_internal::GetRepHi(d));
if (ts.tv_sec == time_internal::GetRepHi(d)) { // no time_t narrowing if (ts.tv_sec == time_internal::GetRepHi(d)) { // no time_t narrowing
ts.tv_nsec = time_internal::GetRepLo(d) / 4; // floor ts.tv_nsec = time_internal::GetRepLo(d) / 4; // floor
return ts; return ts;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment