Add a debug-only check for depth_ match, to catch a problem that is difficult

to diagnose otherwise.

PiperOrigin-RevId: 485688363
pull/10890/head
Protobuf Team Bot 2 years ago committed by Copybara-Service
parent 46d1145ec8
commit dc8c98007a
  1. 2
      src/google/protobuf/parse_context.cc
  2. 16
      src/google/protobuf/parse_context.h

@ -278,7 +278,9 @@ const char* ParseContext::ParseMessage(MessageLite* msg, const char* ptr) {
int old; int old;
ptr = ReadSizeAndPushLimitAndDepth(ptr, &old); ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
if (ptr == nullptr) return ptr; if (ptr == nullptr) return ptr;
auto old_depth = depth_;
ptr = msg->_InternalParse(ptr, this); ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++; depth_++;
if (!PopLimit(old)) return nullptr; if (!PopLimit(old)) return nullptr;
return ptr; return ptr;

@ -445,7 +445,9 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
MessageLite* msg, const char* ptr, const Table* table) { MessageLite* msg, const char* ptr, const Table* table) {
int old; int old;
ptr = ReadSizeAndPushLimitAndDepthInlined(ptr, &old); ptr = ReadSizeAndPushLimitAndDepthInlined(ptr, &old);
auto old_depth = depth_;
ptr = ptr ? TcParser::ParseLoop(msg, ptr, this, table) : nullptr; ptr = ptr ? TcParser::ParseLoop(msg, ptr, this, table) : nullptr;
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++; depth_++;
if (!PopLimit(old)) return nullptr; if (!PopLimit(old)) return nullptr;
return ptr; return ptr;
@ -456,7 +458,13 @@ class PROTOBUF_EXPORT ParseContext : public EpsCopyInputStream {
T* msg, const char* ptr, uint32_t tag) { T* msg, const char* ptr, uint32_t tag) {
if (--depth_ < 0) return nullptr; if (--depth_ < 0) return nullptr;
group_depth_++; group_depth_++;
auto old_depth = depth_;
auto old_group_depth = group_depth_;
ptr = msg->_InternalParse(ptr, this); ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) {
GOOGLE_DCHECK_EQ(old_depth, depth_);
GOOGLE_DCHECK_EQ(old_group_depth, group_depth_);
}
group_depth_--; group_depth_--;
depth_++; depth_++;
if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr; 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) { MessageLite* msg, const char* ptr, uint32_t tag, const Table* table) {
if (--depth_ < 0) return nullptr; if (--depth_ < 0) return nullptr;
group_depth_++; group_depth_++;
auto old_depth = depth_;
auto old_group_depth = group_depth_;
ptr = TcParser::ParseLoop(msg, ptr, this, table); 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_--; group_depth_--;
depth_++; depth_++;
if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr; if (PROTOBUF_PREDICT_FALSE(!ConsumeEndGroup(tag))) return nullptr;
@ -835,7 +849,9 @@ PROTOBUF_NODISCARD const char* ParseContext::ParseMessage(T* msg,
int old; int old;
ptr = ReadSizeAndPushLimitAndDepth(ptr, &old); ptr = ReadSizeAndPushLimitAndDepth(ptr, &old);
if (ptr == nullptr) return ptr; if (ptr == nullptr) return ptr;
auto old_depth = depth_;
ptr = msg->_InternalParse(ptr, this); ptr = msg->_InternalParse(ptr, this);
if (ptr != nullptr) GOOGLE_DCHECK_EQ(old_depth, depth_);
depth_++; depth_++;
if (!PopLimit(old)) return nullptr; if (!PopLimit(old)) return nullptr;
return ptr; return ptr;

Loading…
Cancel
Save