Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(demangle): limit recursion depth #958

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/demangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ struct State {
short nest_level; // For nested names.
bool append; // Append flag.
bool overflowed; // True if output gets overflowed.
uint32 local_level;
uint32 expr_level;
};

// We don't use strlen() in libc since it's not guaranteed to be async
Expand Down Expand Up @@ -155,6 +157,8 @@ static void InitState(State *state, const char *mangled,
state->nest_level = -1;
state->append = true;
state->overflowed = false;
state->local_level = 0;
state->expr_level = 0;
}

// Returns true and advances "mangled_cur" if we find "one_char_token"
Expand Down Expand Up @@ -1127,42 +1131,56 @@ static bool ParseExpression(State *state) {
return true;
}

// Avoid recursion above max_levels
constexpr uint32 max_levels = 5;

if (state->expr_level > max_levels) {
return false;
}
++state->expr_level;

State copy = *state;
if (ParseOperatorName(state) &&
ParseExpression(state) &&
ParseExpression(state) &&
ParseExpression(state)) {
--state->expr_level;
return true;
}
*state = copy;

if (ParseOperatorName(state) &&
ParseExpression(state) &&
ParseExpression(state)) {
--state->expr_level;
return true;
}
*state = copy;

if (ParseOperatorName(state) &&
ParseExpression(state)) {
--state->expr_level;
return true;
}
*state = copy;

if (ParseTwoCharToken(state, "st") && ParseType(state)) {
return true;
--state->expr_level;
}
*state = copy;

if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
ParseUnqualifiedName(state) &&
ParseTemplateArgs(state)) {
--state->expr_level;
return true;
}
*state = copy;

if (ParseTwoCharToken(state, "sr") && ParseType(state) &&
ParseUnqualifiedName(state)) {
--state->expr_level;
return true;
}
*state = copy;
Expand Down Expand Up @@ -1208,16 +1226,25 @@ static bool ParseExprPrimary(State *state) {
// [<discriminator>]
// := Z <(function) encoding> E s [<discriminator>]
static bool ParseLocalName(State *state) {
// Avoid recursion above max_levels
constexpr uint32 max_levels = 5;
if (state->local_level > max_levels) {
return false;
}
++state->local_level;

State copy = *state;
if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
ParseOneCharToken(state, 'E') && MaybeAppend(state, "::") &&
ParseName(state) && Optional(ParseDiscriminator(state))) {
--state->local_level;
return true;
}
*state = copy;

if (ParseOneCharToken(state, 'Z') && ParseEncoding(state) &&
ParseTwoCharToken(state, "Es") && Optional(ParseDiscriminator(state))) {
--state->local_level;
return true;
}
*state = copy;
Expand Down
Loading