From 9081bae6233bebdb26b2f617faf51fb8624aaeef Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Mon, 31 Aug 2026 18:28:40 -0700 Subject: [PATCH] Internal change PiperOrigin-RevId: 974191958 --- src/google/protobuf/parse_context.cc | 8 -------- src/google/protobuf/parse_context.h | 26 +++++++++++++++++++------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/google/protobuf/parse_context.cc b/src/google/protobuf/parse_context.cc index cdafc0b3ed872..8b5ece10e9056 100644 --- a/src/google/protobuf/parse_context.cc +++ b/src/google/protobuf/parse_context.cc @@ -86,15 +86,7 @@ bool ParsingEndsInBuffer(const char* ptr, const char* end, int depth) { } } // namespace -bool EpsCopyInputStream::IsRequestedLessThanOrEqualTo(int requested, - int available) { - return static_cast(static_cast(requested)) <= - static_cast(available); -} -bool EpsCopyInputStream::CanReadFromPtr(int requested, const char* ptr) { - return IsRequestedLessThanOrEqualTo(requested, BytesAvailable(ptr)); -} bool EpsCopyInputStream::HasEnoughTillLimit(int requested, const char* ptr) { return IsRequestedLessThanOrEqualTo(requested, BytesUntilLimit(ptr)); diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h index ec4264c7a28ba..3615de4726c59 100644 --- a/src/google/protobuf/parse_context.h +++ b/src/google/protobuf/parse_context.h @@ -566,12 +566,19 @@ class PROTOBUF_EXPORT EpsCopyInputStream { // Returns true if it has enough available data given requested. Note that // "available" can be negative but "requested" must not. Casting is done to // preserve sign bit for the latter only. - bool IsRequestedLessThanOrEqualTo(int requested, int available); + PROTOBUF_ALWAYS_INLINE bool IsRequestedLessThanOrEqualTo( + int requested, int available) const { + return static_cast(static_cast(requested)) <= + static_cast(available); + } // Returns true if "requested" bytes can be read contiguously from "ptr". Note // that negative "requested" is converted to uint32_t before comparison, which // will cause failure. - bool CanReadFromPtr(int requested, const char* ptr); + PROTOBUF_ALWAYS_INLINE bool CanReadFromPtr(int requested, + const char* ptr) const { + return IsRequestedLessThanOrEqualTo(requested, BytesAvailable(ptr)); + } // Returns true if "requested" bytes are avilable till limit. Note that // negative "requested" is converted to uint32_t before comparison. @@ -1384,13 +1391,18 @@ std::pair ReadSizeFallback(const char* p, uint32_t res); // otherwise returns nullptr. Caller must ensure it is safe to call. PROTOBUF_FUTURE_ADD_EARLY_NODISCARD inline uint32_t ReadSize(const char** pp) { - auto p = *pp; - uint32_t res = static_cast(p[0]); - if (res < 128) { + const char* p = *pp; + uint32_t b0 = static_cast(p[0]); + if (ABSL_PREDICT_TRUE(b0 < 128)) { *pp = p + 1; - return res; + return b0; + } + uint32_t b1 = static_cast(p[1]); + if (ABSL_PREDICT_TRUE(b1 < 128)) { + *pp = p + 2; + return (b0 & 0x7F) | (b1 << 7); } - auto x = ReadSizeFallback(p, res); + auto x = ReadSizeFallback(p, b0); *pp = x.first; return x.second; }