Skip to content

Commit

Permalink
Add a debug-only check for depth_ match, to catch a problem that is d…
Browse files Browse the repository at this point in the history
…ifficult

to diagnose otherwise.

PiperOrigin-RevId: 485688363
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 2, 2022
1 parent 46d1145 commit dc8c980
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/google/protobuf/parse_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
int old;
ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
if (ptr == nullptr) return ptr;
auto old_depth = depth_;
ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++;
if (!PopLimit(old)) return nullptr;
return ptr;
Expand Down
16 changes: 16 additions & 0 deletions src/google/protobuf/parse_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,9 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
MessageLite* msg, const char* ptr, const Table* table) {
int old;
ptr = ReadSizeAndPushLimitAndDepthInlined(ptr, &old);
auto old_depth = depth_;
ptr = ptr ? TcParser::ParseLoop(msg, ptr, this, table) : nullptr;
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++;
if (!PopLimit(old)) return nullptr;
return ptr;
Expand All @@ -456,7 +458,13 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
T* msg, const char* ptr, uint32_t tag) {
if (--depth_ < 0) return nullptr;
group_depth_++;
auto old_depth = depth_;
auto old_group_depth = group_depth_;
ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) {
GOOGLE_DCHECK_EQ(old_depth, depth_);
GOOGLE_DCHECK_EQ(old_group_depth, group_depth_);
}
group_depth_--;
depth_++;
if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr;
Expand All @@ -468,7 +476,13 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
MessageLite* msg, const char* ptr, uint32_t tag, const Table* table) {
if (--depth_ < 0) return nullptr;
group_depth_++;
auto old_depth = depth_;
auto old_group_depth = group_depth_;
ptr = TcParser::ParseLoop(msg, ptr, this, table);
if (ptr != nullptr) {
GOOGLE_DCHECK_EQ(old_depth, depth_);
GOOGLE_DCHECK_EQ(old_group_depth, group_depth_);
}
group_depth_--;
depth_++;
if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr;
Expand Down Expand Up @@ -835,7 +849,9 @@ PROTOBUF_NODISCARD const char* ParseContext::ParseMessage(T* msg,
int old;
ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
if (ptr == nullptr) return ptr;
auto old_depth = depth_;
ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++;
if (!PopLimit(old)) return nullptr;
return ptr;
Expand Down

0 comments on commit dc8c980

Please sign in to comment.