Skip to content

Commit

Permalink
fix(demangle): limit recursion depth
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Oct 5, 2023
1 parent 319a0df commit eab1edb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/demangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ struct State {
short nest_level; // For nested names.
bool append; // Append flag.
bool overflowed; // True if output gets overflowed.
uint32 local_level;
};

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

// Returns true and advances "mangled_cur" if we find "one_char_token"
Expand Down Expand Up @@ -1208,16 +1210,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

0 comments on commit eab1edb

Please sign in to comment.