Skip to content

Commit

Permalink
Change some C-arrays to std::array as this enables bounds checking
Browse files Browse the repository at this point in the history
in some hardened standard library builds

For example https://libcxx.llvm.org/Hardening.html

PiperOrigin-RevId: 700386333
Change-Id: Id839ab6b1daa74d7f80c3309dfa9a21076450cea
  • Loading branch information
derekmauro authored and copybara-github committed Nov 26, 2024
1 parent c05bceb commit c7cf999
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
14 changes: 7 additions & 7 deletions absl/strings/escaping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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, // ", '
Expand Down Expand Up @@ -481,7 +481,7 @@ void CEscapeAndAppendInternal(absl::string_view src,
// 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 = '.';
Expand Down Expand Up @@ -746,7 +746,7 @@ bool Base64UnescapeInternal(absl::Nullable<const char*> 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<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,
Expand Down Expand Up @@ -781,7 +781,7 @@ constexpr signed char kUnBase64[] = {
-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,
Expand Down Expand Up @@ -820,7 +820,7 @@ constexpr signed char kUnWebSafeBase64[] = {
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);
Expand All @@ -845,7 +845,7 @@ bool Base64UnescapeInternal(absl::Nullable<const char*> src, size_t slen,
}

/* 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,
Expand All @@ -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<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,
Expand Down
5 changes: 3 additions & 2 deletions absl/strings/internal/str_format/float_conversion.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <string.h>

#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <limits>
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -234,7 +235,7 @@ class BinaryToDecimal {
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_;
Expand Down
3 changes: 2 additions & 1 deletion absl/strings/numbers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<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,
Expand Down

0 comments on commit c7cf999

Please sign in to comment.