diff --git a/src/google/protobuf/parse_context.cc b/src/google/protobuf/parse_context.cc index 7bd935f86f..9e591ad91d 100644 --- a/src/google/protobuf/parse_context.cc +++ b/src/google/protobuf/parse_context.cc @@ -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; diff --git a/src/google/protobuf/parse_context.h b/src/google/protobuf/parse_context.h index 627a0689de..182f3af48d 100644 --- a/src/google/protobuf/parse_context.h +++ b/src/google/protobuf/parse_context.h @@ -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; @@ -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; @@ -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; @@ -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;