Skip to content

Commit dcc0380

Browse files
Fix #546 fuzzing crash in simplecpp::preprocess() (#553)
1 parent d86671f commit dcc0380

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

simplecpp.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,9 +3602,11 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
36023602
header = tok->str().substr(1U, tok->str().size() - 2U);
36033603
closingAngularBracket = true;
36043604
}
3605-
std::ifstream f;
3606-
const std::string header2 = openHeader(f,dui,sourcefile,header,systemheader);
3607-
expr.push_back(new Token(header2.empty() ? "0" : "1", tok->location));
3605+
if (tok) {
3606+
std::ifstream f;
3607+
const std::string header2 = openHeader(f,dui,sourcefile,header,systemheader);
3608+
expr.push_back(new Token(header2.empty() ? "0" : "1", tok->location));
3609+
}
36083610
}
36093611
if (par)
36103612
tok = tok ? tok->next : nullptr;

test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,6 +3259,12 @@ static void fuzz_crash()
32593259
"foo(f##oo(intp))\n";
32603260
(void)preprocess(code, simplecpp::DUI()); // do not crash
32613261
}
3262+
{ // #546
3263+
const char code[] = "#if __has_include<\n";
3264+
simplecpp::OutputList outputList;
3265+
ASSERT_EQUALS("", preprocess(code, &outputList)); // do not crash
3266+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition\n", toString(outputList));
3267+
}
32623268
}
32633269

32643270
// memory leaks detected by LSAN/valgrind

0 commit comments

Comments
 (0)