This is an automated email from the ASF dual-hosted git repository.

chenBright pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new f00833da1 skip deleted mcpack fields without recursing in operator++ 
(#3762)
f00833da1 is described below

commit f00833da1214d763dab8c9034039cb5560d25185
Author: sahvx655-wq <[email protected]>
AuthorDate: Sun Nov 29 06:54:29 2026 +0530

    skip deleted mcpack fields without recursing in operator++ (#3762)
---
 src/mcpack2pb/parser.cpp         | 22 ++++++++++++++++------
 test/brpc_mcpack2pb_unittest.cpp | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/src/mcpack2pb/parser.cpp b/src/mcpack2pb/parser.cpp
index 5cfb2bd99..c73b38f5b 100644
--- a/src/mcpack2pb/parser.cpp
+++ b/src/mcpack2pb/parser.cpp
@@ -101,6 +101,11 @@ void ObjectIterator::operator++() {
             return set_bad();
         }
     }
+    // Deleted fields are skipped by jumping back here rather than by calling
+    // operator++() again: they are counted by bytes, not by item_count, so a
+    // payload made of many of them would otherwise recurse once per field and
+    // overflow the stack.
+next_field:
     if (_expected_popped_bytes >= _expected_popped_end) {
         return set_end();
     }
@@ -116,7 +121,7 @@ void ObjectIterator::operator++() {
             - sizeof(FieldFixedHead);
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             _stream->popn(head.full_size() - sizeof(FieldFixedHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         _current_field.name = _stream->ref_cut(&_name_backup_string, 
head.name_size());
         if (!_current_field.name.empty()) {
@@ -135,7 +140,7 @@ void ObjectIterator::operator++() {
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             // Skip deleted field.
             _stream->popn(head.full_size() - sizeof(FieldShortHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         // Remove FIELD_SHORT_MASK.
         FieldType type = (FieldType)(head.type() & ~FIELD_SHORT_MASK);
@@ -156,7 +161,7 @@ void ObjectIterator::operator++() {
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             // Skip deleted field.
             _stream->popn(head.full_size() - sizeof(FieldLongHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         _current_field.name = _stream->ref_cut(&_name_backup_string, 
head.name_size());
         if (!_current_field.name.empty()) {
@@ -184,6 +189,11 @@ void ArrayIterator::operator++() {
             return set_bad();
         }
     }
+    // Deleted fields are skipped by jumping back here rather than by calling
+    // operator++() again: they are counted by bytes, not by item_count, so a
+    // payload made of many of them would otherwise recurse once per field and
+    // overflow the stack.
+next_field:
     if (_expected_popped_bytes >= _expected_popped_end) {
         return set_end();
     }
@@ -200,7 +210,7 @@ void ArrayIterator::operator++() {
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             // Skip deleted fields.
             _stream->popn(head.full_size() - sizeof(FieldFixedHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         const uint32_t name_size = head.name_size();
         if (name_size) {
@@ -219,7 +229,7 @@ void ArrayIterator::operator++() {
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             // Skip deleted field.
             _stream->popn(head.full_size() - sizeof(FieldShortHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         // Remove FIELD_SHORT_MASK.
         const FieldType type = (FieldType)(head.type() & ~FIELD_SHORT_MASK);
@@ -240,7 +250,7 @@ void ArrayIterator::operator++() {
         if (!(head.type() & FIELD_NON_DELETED_MASK)) {
             // Skip deleted field.
             _stream->popn(head.full_size() - sizeof(FieldLongHead));
-            return operator++();  // tailr
+            goto next_field;
         }
         const uint32_t name_size = head.name_size();
         if (name_size) {
diff --git a/test/brpc_mcpack2pb_unittest.cpp b/test/brpc_mcpack2pb_unittest.cpp
index b40a30c46..55c406566 100644
--- a/test/brpc_mcpack2pb_unittest.cpp
+++ b/test/brpc_mcpack2pb_unittest.cpp
@@ -297,6 +297,40 @@ TEST(Mcpack2pbParserTest, ModeratelyNestedPayloadParses) {
     EXPECT_TRUE(ok);
 }
 
+static void AppendDeletedFields(std::string* out, int count) {
+    for (int i = 0; i < count; ++i) {
+        out->push_back((char)0x80);  // FIELD_SHORT_MASK, deleted (type & 0x70 
== 0)
+        out->push_back(0x00);        // name_size = 0
+        out->push_back(0x00);        // value_size = 0
+    }
+}
+
+TEST(Mcpack2pbParserTest, ManyDeletedFieldsDoNotOverflowStack) {
+    // Deleted fields are skipped by size and are not bounded by item_count
+    // or MAX_DEPTH. Skipping them used to recurse into operator++() once per
+    // field, so ~100K of them (a 600KB payload, below the default
+    // max_body_size) overflowed the 1 MB stack. Cover both ObjectIterator
+    // and ArrayIterator.
+    const int kDeleted = 100000;
+    const std::string name = std::string("children\0", 9);
+    std::string arr;  // an array with only deleted items
+    AppendU32(&arr, 0);
+    AppendDeletedFields(&arr, kDeleted);
+
+    std::string payload;  // an object: deleted fields, then "children"
+    AppendU32(&payload, 1);
+    AppendDeletedFields(&payload, kDeleted);
+    payload.push_back(0x20);  // FIELD_ARRAY
+    payload.push_back((char)name.size());
+    AppendU32(&payload, (uint32_t)arr.size());
+    payload.append(name);
+    payload.append(arr);
+
+    bool ok = false;
+    ASSERT_EQ(0, ParseRecursivePayloadWith1MBStack(payload, &ok));
+    EXPECT_TRUE(ok);
+}
+
 TEST(Mcpack2pbParserTest, ObjectItemCountIsRejectedWhenInconsistentWithSize) {
     // An mcpack object whose ItemsHead declares an absurd field count for
     // the given payload must be rejected instead of being trusted: the


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to