Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/google/protobuf/parse_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(static_cast<uint32_t>(requested)) <=
static_cast<int64_t>(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));
Expand Down
26 changes: 19 additions & 7 deletions src/google/protobuf/parse_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int64_t>(static_cast<uint32_t>(requested)) <=
static_cast<int64_t>(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.
Expand Down Expand Up @@ -1384,13 +1391,18 @@ std::pair<const char*, int32_t> 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<uint8_t>(p[0]);
if (res < 128) {
const char* p = *pp;
uint32_t b0 = static_cast<uint8_t>(p[0]);
if (ABSL_PREDICT_TRUE(b0 < 128)) {
*pp = p + 1;
return res;
return b0;
}
uint32_t b1 = static_cast<uint8_t>(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;
}
Expand Down
Loading