diff options
author | Abseil Team <absl-team@google.com> | 2022-08-01 08:29:37 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2022-08-01 08:30:25 -0700 |
commit | 16af2bbcb9dd1770c64483aed8cd7d4ae7061064 (patch) | |
tree | 358b0bc9ba9786d4af2f49546d2b06868e80e568 /absl/base | |
parent | dc370a82467cb35066475537b797197aee3e5164 (diff) |
Fix "unsafe narrowing" warnings in absl, 2/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 dirs a-h.)
Bug: chromium:1292951
PiperOrigin-RevId: 464541951
Change-Id: If23b63ccea8e9b730159ff1c7288e9300a40b6bd
Diffstat (limited to 'absl/base')
-rw-r--r-- | absl/base/internal/low_level_alloc.cc | 2 | ||||
-rw-r--r-- | absl/base/internal/low_level_alloc.h | 2 | ||||
-rw-r--r-- | absl/base/internal/raw_logging.cc | 12 | ||||
-rw-r--r-- | absl/base/internal/strerror.cc | 8 | ||||
-rw-r--r-- | absl/base/internal/sysinfo.cc | 4 |
5 files changed, 16 insertions, 12 deletions
diff --git a/absl/base/internal/low_level_alloc.cc b/absl/base/internal/low_level_alloc.cc index 229ab916..aa6c3c3c 100644 --- a/absl/base/internal/low_level_alloc.cc +++ b/absl/base/internal/low_level_alloc.cc @@ -364,7 +364,7 @@ LowLevelAlloc::Arena::Arena(uint32_t flags_value) } // L < meta_data_arena->mu -LowLevelAlloc::Arena *LowLevelAlloc::NewArena(int32_t flags) { +LowLevelAlloc::Arena *LowLevelAlloc::NewArena(uint32_t flags) { Arena *meta_data_arena = DefaultArena(); #ifndef ABSL_LOW_LEVEL_ALLOC_ASYNC_SIGNAL_SAFE_MISSING if ((flags & LowLevelAlloc::kAsyncSignalSafe) != 0) { diff --git a/absl/base/internal/low_level_alloc.h b/absl/base/internal/low_level_alloc.h index db91951c..eabb14a9 100644 --- a/absl/base/internal/low_level_alloc.h +++ b/absl/base/internal/low_level_alloc.h @@ -103,7 +103,7 @@ class LowLevelAlloc { // the provided flags. For example, the call NewArena(kAsyncSignalSafe) // is itself async-signal-safe, as well as generatating an arena that provides // async-signal-safe Alloc/Free. - static Arena *NewArena(int32_t flags); + static Arena *NewArena(uint32_t flags); // Destroys an arena allocated by NewArena and returns true, // provided no allocated blocks remain in the arena. diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc index a5d9fa59..6273e847 100644 --- a/absl/base/internal/raw_logging.cc +++ b/absl/base/internal/raw_logging.cc @@ -89,12 +89,14 @@ constexpr char kTruncated[] = " ... (message truncated)\n"; bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) ABSL_PRINTF_ATTRIBUTE(3, 0); bool VADoRawLog(char** buf, int* size, const char* format, va_list ap) { - int n = vsnprintf(*buf, *size, format, ap); + if (*size < 0) + return false; + int n = vsnprintf(*buf, static_cast<size_t>(*size), format, ap); bool result = true; if (n < 0 || n > *size) { result = false; if (static_cast<size_t>(*size) > sizeof(kTruncated)) { - n = *size - sizeof(kTruncated); // room for truncation message + n = *size - static_cast<int>(sizeof(kTruncated)); } else { n = 0; // no room for truncation message } @@ -116,9 +118,11 @@ constexpr int kLogBufSize = 3000; bool DoRawLog(char** buf, int* size, const char* format, ...) ABSL_PRINTF_ATTRIBUTE(3, 4); bool DoRawLog(char** buf, int* size, const char* format, ...) { + if (*size < 0) + return false; va_list ap; va_start(ap, format); - int n = vsnprintf(*buf, *size, format, ap); + int n = vsnprintf(*buf, static_cast<size_t>(*size), format, ap); va_end(ap); if (n < 0 || n > *size) return false; *size -= n; @@ -206,7 +210,7 @@ void AsyncSignalSafeWriteToStderr(const char* s, size_t len) { #elif defined(ABSL_HAVE_POSIX_WRITE) write(STDERR_FILENO, s, len); #elif defined(ABSL_HAVE_RAW_IO) - _write(/* stderr */ 2, s, len); + _write(/* stderr */ 2, s, static_cast<unsigned>(len)); #else // stderr logging unsupported on this platform (void) s; diff --git a/absl/base/internal/strerror.cc b/absl/base/internal/strerror.cc index 0d6226fd..de91c05e 100644 --- a/absl/base/internal/strerror.cc +++ b/absl/base/internal/strerror.cc @@ -66,8 +66,8 @@ constexpr int kSysNerr = 135; std::array<std::string, kSysNerr>* NewStrErrorTable() { auto* table = new std::array<std::string, kSysNerr>; - for (int i = 0; i < static_cast<int>(table->size()); ++i) { - (*table)[i] = StrErrorInternal(i); + for (size_t i = 0; i < table->size(); ++i) { + (*table)[i] = StrErrorInternal(static_cast<int>(i)); } return table; } @@ -77,8 +77,8 @@ std::array<std::string, kSysNerr>* NewStrErrorTable() { std::string StrError(int errnum) { absl::base_internal::ErrnoSaver errno_saver; static const auto* table = NewStrErrorTable(); - if (errnum >= 0 && errnum < static_cast<int>(table->size())) { - return (*table)[errnum]; + if (errnum >= 0 && static_cast<size_t>(errnum) < table->size()) { + return (*table)[static_cast<size_t>(errnum)]; } return StrErrorInternal(errnum); } diff --git a/absl/base/internal/sysinfo.cc b/absl/base/internal/sysinfo.cc index c8366df1..a3e08b93 100644 --- a/absl/base/internal/sysinfo.cc +++ b/absl/base/internal/sysinfo.cc @@ -117,7 +117,7 @@ int Win32NumCPUs() { } } free(info); - return logicalProcessorCount; + return static_cast<int>(logicalProcessorCount); } #endif @@ -128,7 +128,7 @@ static int GetNumCPUs() { #if defined(__myriad2__) return 1; #elif defined(_WIN32) - const unsigned hardware_concurrency = Win32NumCPUs(); + const int hardware_concurrency = Win32NumCPUs(); return hardware_concurrency ? hardware_concurrency : 1; #elif defined(_AIX) return sysconf(_SC_NPROCESSORS_ONLN); |