Skip to content

Fix bug when parsing zero-length string field in mcpack2pb - #3450

Open
wwbmmm wants to merge 3 commits into
apache:masterfrom
wwbmmm:fix-mcpack2pb-zero-length-string
Open

Fix bug when parsing zero-length string field in mcpack2pb#3450
wwbmmm wants to merge 3 commits into
apache:masterfrom
wwbmmm:fix-mcpack2pb-zero-length-string

Conversation

@wwbmmm

@wwbmmm wwbmmm commented Aug 15, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: null

Problem Summary:

A malformed mcpack2/compack request that contains a string field with a
zero value_size crashes the server. In UnparsedValue::as_string(),
_size (the string field's value_size, taken directly from the input)
is used in out->resize(_size - 1) without being validated first. When
value_size is 0, _size - 1 underflows to SIZE_MAX, resize() throws
std::length_error, and since the exception is not caught anywhere on
the request parsing path the whole process aborts. This is reachable
from UbrpcAdaptor::ParseNsheadMeta and
UbrpcAdaptor::ParseRequestFromIOBuf for servers wired with the
nshead + ubrpc mcpack2 adaptor.

What is changed and the side effects?

Changed:

  • src/mcpack2pb/parser.cpp: reject string fields whose value_size is 0
    in UnparsedValue::as_string() by marking the input stream bad and
    returning, instead of underflowing and throwing.
  • test/brpc_mcpack2pb_unittest.cpp: add unit tests covering the
    zero-size string field case and a normal string field parse.

Side effects:

  • Performance effects: none (one extra comparison per string field).
  • Breaking backward compatibility: none. Well-formed input is parsed
    exactly as before; only previously-crashing malformed input now fails
    gracefully.

Check List:

UnparsedValue::as_string() resizes the output string to
without checking , where  is the value_size of a string
field read from the input. When value_size is 0,  underflows
to SIZE_MAX and resize() throws std::length_error, which is not caught
on the request path and therefore crashes the server. Reject such
malformed fields by marking the stream bad so the caller can fail the
request gracefully instead.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash in the mcpack2pb request-parsing path when encountering malformed string fields with value_size == 0, improving robustness of servers using the nshead + ubrpc mcpack2 adaptor.

Changes:

  • Add validation in mcpack2pb::UnparsedValue::as_string() to reject zero-length string fields (prevents _size - 1 underflow and uncaught std::length_error).
  • Add unit tests that cover the malformed zero-size string case and a normal string parse.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/mcpack2pb/parser.cpp Adds guard for _size == 0 in UnparsedValue::as_string() to avoid underflow/exception.
test/brpc_mcpack2pb_unittest.cpp Introduces parser unit tests for malformed zero-size string field and a valid string field.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/mcpack2pb/parser.cpp

@chenBright chenBright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants