diff options
author | Derek Mauro <dmauro@google.com> | 2024-03-14 08:44:16 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-03-14 08:45:13 -0700 |
commit | 74df6975aef6d6fa2020313922e9a94b42364f38 (patch) | |
tree | 32aef1c8ec91cee317e18eeffb7a1c1cadcc7d1c /absl/strings/internal/escaping.cc | |
parent | 2f0591010da73a6157f515170b0ef349d9338003 (diff) | |
download | abseil-74df6975aef6d6fa2020313922e9a94b42364f38.tar.gz abseil-74df6975aef6d6fa2020313922e9a94b42364f38.tar.bz2 abseil-74df6975aef6d6fa2020313922e9a94b42364f38.zip |
Add additional checks for size_t overflows
This change mainly affects 32-bit platforms. Similar to
4618865caf8596742a9fd7c28a70a46b5e277794, check for size_t overflow
in all places where string result sizes are precomputed before allocation.
PiperOrigin-RevId: 615792028
Change-Id: I71c774c5ef2c2978bd812c70e9bab36d266b7c90
Diffstat (limited to 'absl/strings/internal/escaping.cc')
-rw-r--r-- | absl/strings/internal/escaping.cc | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/absl/strings/internal/escaping.cc b/absl/strings/internal/escaping.cc index 56a4cbed..d2abe669 100644 --- a/absl/strings/internal/escaping.cc +++ b/absl/strings/internal/escaping.cc @@ -14,6 +14,8 @@ #include "absl/strings/internal/escaping.h" +#include <limits> + #include "absl/base/internal/endian.h" #include "absl/base/internal/raw_logging.h" @@ -31,12 +33,14 @@ ABSL_CONST_INIT const char kBase64Chars[] = ABSL_CONST_INIT const char kWebSafeBase64Chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; - size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding) { // Base64 encodes three bytes of input at a time. If the input is not // divisible by three, we pad as appropriate. // // Base64 encodes each three bytes of input into four bytes of output. + constexpr size_t kMaxSize = (std::numeric_limits<size_t>::max() - 1) / 4 * 3; + ABSL_INTERNAL_CHECK(input_len <= kMaxSize, + "CalculateBase64EscapedLenInternal() overflow"); size_t len = (input_len / 3) * 4; // Since all base 64 input is an integral number of octets, only the following @@ -66,7 +70,6 @@ size_t CalculateBase64EscapedLenInternal(size_t input_len, bool do_padding) { } } - assert(len >= input_len); // make sure we didn't overflow return len; } |