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';
} }
......
...@@ -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