Skip to content

Commit fd86aa7

Browse files
derekmaurocopybara-github
authored andcommitted
Fix buffer overflow the internal demangling function
The overflow can happen during rollback after a parsing failure, where the null terminator is written without verifying the buffer bounds. Credit to www.code-intelligence.com for reporting this issue PiperOrigin-RevId: 732995553 Change-Id: Ic5075f53e510d270e1784d593defcd53f9121d02
1 parent ca210f1 commit fd86aa7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

absl/debugging/internal/demangle.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,8 @@ static bool ParseLocalNameSuffix(State *state) {
28192819
// On late parse failure, roll back not only the input but also the output,
28202820
// whose trailing NUL was overwritten.
28212821
state->parse_state = copy;
2822-
if (state->parse_state.append) {
2822+
if (state->parse_state.append &&
2823+
state->parse_state.out_cur_idx < state->out_end_idx) {
28232824
state->out[state->parse_state.out_cur_idx] = '\0';
28242825
}
28252826
return false;
@@ -2832,7 +2833,8 @@ static bool ParseLocalNameSuffix(State *state) {
28322833
return true;
28332834
}
28342835
state->parse_state = copy;
2835-
if (state->parse_state.append) {
2836+
if (state->parse_state.append &&
2837+
state->parse_state.out_cur_idx < state->out_end_idx) {
28362838
state->out[state->parse_state.out_cur_idx] = '\0';
28372839
}
28382840

absl/debugging/internal/demangle_test.cc

+7
Original file line numberDiff line numberDiff line change
@@ -2017,6 +2017,13 @@ TEST(DemangleRegression, DeeplyNestedArrayType) {
20172017
TestOnInput(data.c_str());
20182018
}
20192019

2020+
TEST(DemangleRegression, ShortOutputBuffer) {
2021+
// This should not crash.
2022+
char buffer[1];
2023+
EXPECT_FALSE(
2024+
absl::debugging_internal::Demangle("_ZZ2wwE", buffer, sizeof(buffer)));
2025+
}
2026+
20202027
struct Base {
20212028
virtual ~Base() = default;
20222029
};

0 commit comments

Comments
 (0)