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
diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc
index 8f5dbcf..b70c504 100644
--- a/absl/strings/escaping.cc
+++ b/absl/strings/escaping.cc
@@ -370,7 +370,7 @@
}
/* clang-format off */
-constexpr unsigned char kCEscapedLen[256] = {
+constexpr std::array<unsigned char, 256> 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 @@
// documentation for details of the mapping.
bool Base64UnescapeInternal(absl::Nullable<const char*> src_param, size_t szsrc,
absl::Nullable<char*> dest, size_t szdest,
- absl::Nonnull<const signed char*> unbase64,
+ const std::array<signed char, 256>& unbase64,
absl::Nonnull<size_t*> len) {
static const char kPad64Equals = '=';
static const char kPad64Dot = '.';
@@ -746,7 +746,7 @@
// 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<signed char, 256> 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 @@
-1, -1, -1, -1, -1, -1, -1, -1
};
-constexpr signed char kUnWebSafeBase64[] = {
+constexpr std::array<signed char, 256> 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 @@
template <typename String>
bool Base64UnescapeInternal(absl::Nullable<const char*> src, size_t slen,
absl::Nonnull<String*> dest,
- absl::Nonnull<const signed char*> unbase64) {
+ const std::array<signed char, 256>& 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 @@
}
/* clang-format off */
-constexpr char kHexValueLenient[256] = {
+constexpr std::array<char, 256> 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 @@
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
-constexpr signed char kHexValueStrict[256] = {
+constexpr std::array<signed char, 256> 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 8edf520..aa31998 100644
--- a/absl/strings/internal/str_format/float_conversion.cc
+++ b/absl/strings/internal/str_format/float_conversion.cc
@@ -17,6 +17,7 @@
#include <string.h>
#include <algorithm>
+#include <array>
#include <cassert>
#include <cmath>
#include <limits>
@@ -159,7 +160,7 @@
// 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 @@
size_t decimal_start_;
size_t decimal_end_;
- char digits_[kDigitsPerChunk];
+ std::array<char, kDigitsPerChunk> digits_;
size_t size_ = 0;
absl::Span<uint32_t> data_;
diff --git a/absl/strings/numbers.cc b/absl/strings/numbers.cc
index b57d9e8..83ea80b 100644
--- a/absl/strings/numbers.cc
+++ b/absl/strings/numbers.cc
@@ -18,6 +18,7 @@
#include "absl/strings/numbers.h"
#include <algorithm>
+#include <array>
#include <cassert>
#include <cfloat> // for DBL_DIG and FLT_DIG
#include <cmath> // for HUGE_VAL
@@ -674,7 +675,7 @@
// 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<int8_t, 256> 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,