From c7cf999bda8390d2dd294ef903716a80135e6f4c Mon Sep 17 00:00:00 2001 From: Derek Mauro Date: Tue, 26 Nov 2024 10:29:23 -0800 Subject: [PATCH] Change some C-arrays to std::array as this enables bounds checking in some hardened standard library builds For example https://libcxx.llvm.org/Hardening.html PiperOrigin-RevId: 700386333 Change-Id: Id839ab6b1daa74d7f80c3309dfa9a21076450cea --- absl/strings/escaping.cc | 14 +++++++------- .../internal/str_format/float_conversion.cc | 5 +++-- absl/strings/numbers.cc | 3 ++- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc index 8f5dbcf59e6..b70c5041ae9 100644 --- a/absl/strings/escaping.cc +++ b/absl/strings/escaping.cc @@ -370,7 +370,7 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, } /* clang-format off */ -constexpr unsigned char kCEscapedLen[256] = { +constexpr std::array kCEscapedLen = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", ' @@ -481,7 +481,7 @@ void CEscapeAndAppendInternal(absl::string_view src, // documentation for details of the mapping. bool Base64UnescapeInternal(absl::Nullable src_param, size_t szsrc, absl::Nullable dest, size_t szdest, - absl::Nonnull unbase64, + const std::array& unbase64, absl::Nonnull len) { static const char kPad64Equals = '='; static const char kPad64Dot = '.'; @@ -746,7 +746,7 @@ bool Base64UnescapeInternal(absl::Nullable src_param, size_t szsrc, // where the value of "Base64[]" was replaced by one of k(WebSafe)Base64Chars // in the internal escaping.cc. /* clang-format off */ -constexpr signed char kUnBase64[] = { +constexpr std::array kUnBase64 = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -781,7 +781,7 @@ constexpr signed char kUnBase64[] = { -1, -1, -1, -1, -1, -1, -1, -1 }; -constexpr signed char kUnWebSafeBase64[] = { +constexpr std::array kUnWebSafeBase64 = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, @@ -820,7 +820,7 @@ constexpr signed char kUnWebSafeBase64[] = { template bool Base64UnescapeInternal(absl::Nullable src, size_t slen, absl::Nonnull dest, - absl::Nonnull unbase64) { + const std::array& unbase64) { // Determine the size of the output string. Base64 encodes every 3 bytes into // 4 characters. Any leftover chars are added directly for good measure. const size_t dest_len = 3 * (slen / 4) + (slen % 4); @@ -845,7 +845,7 @@ bool Base64UnescapeInternal(absl::Nullable src, size_t slen, } /* clang-format off */ -constexpr char kHexValueLenient[256] = { +constexpr std::array kHexValueLenient = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -864,7 +864,7 @@ constexpr char kHexValueLenient[256] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -constexpr signed char kHexValueStrict[256] = { +constexpr std::array kHexValueStrict = { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, diff --git a/absl/strings/internal/str_format/float_conversion.cc b/absl/strings/internal/str_format/float_conversion.cc index 8edf520dcd6..aa319980884 100644 --- a/absl/strings/internal/str_format/float_conversion.cc +++ b/absl/strings/internal/str_format/float_conversion.cc @@ -17,6 +17,7 @@ #include #include +#include #include #include #include @@ -159,7 +160,7 @@ class BinaryToDecimal { // See the current block of digits. absl::string_view CurrentDigits() const { - return absl::string_view(digits_ + kDigitsPerChunk - size_, size_); + return absl::string_view(&digits_[kDigitsPerChunk - size_], size_); } // Advance the current view of digits. @@ -234,7 +235,7 @@ class BinaryToDecimal { size_t decimal_start_; size_t decimal_end_; - char digits_[kDigitsPerChunk]; + std::array digits_; size_t size_ = 0; absl::Span data_; diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc index b57d9e82e5b..83ea80b474b 100644 --- a/absl/strings/numbers.cc +++ b/absl/strings/numbers.cc @@ -18,6 +18,7 @@ #include "absl/strings/numbers.h" #include +#include #include #include // for DBL_DIG and FLT_DIG #include // for HUGE_VAL @@ -674,7 +675,7 @@ namespace { // Represents integer values of digits. // Uses 36 to indicate an invalid character since we support // bases up to 36. -static const int8_t kAsciiToInt[256] = { +static constexpr std::array kAsciiToInt = { 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s. 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0, 1, 2, 3, 4, 5,