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: stack buffer overflow #957

Merged
merged 1 commit into from
Oct 5, 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
9 changes: 9 additions & 0 deletions src/demangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "demangle.h"

#include <cstddef>
#include <cstdio> // for nullptr
#include <limits>

Expand Down Expand Up @@ -222,6 +223,10 @@ static bool ZeroOrMore(ParseFunc parse_func, State *state) {
// is set to true for later use. The output string is ensured to
// always terminate with '\0' as long as there is no overflow.
static void Append(State *state, const char * const str, ssize_t length) {
if (state->out_cur == nullptr) {
state->overflowed = true;
return;
}
for (ssize_t i = 0; i < length; ++i) {
if (state->out_cur + 1 < state->out_end) { // +1 for '\0'
*state->out_cur = str[i];
Expand Down Expand Up @@ -667,6 +672,10 @@ static bool ParseIdentifier(State *state, ssize_t length) {
} else {
MaybeAppendWithLength(state, state->mangled_cur, length);
}
if (length < 0 ||
static_cast<std::size_t>(length) > StrLen(state->mangled_cur)) {
return false;
}
state->mangled_cur += length;
return true;
}
Expand Down
Loading