Skip to content

Commit 6d45cd7

Browse files
authored
fixed danmar#616 - report bad macro syntax via OutputList (danmar#617)
1 parent 10f5052 commit 6d45cd7

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

simplecpp.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3341,8 +3341,21 @@ void simplecpp::preprocess(simplecpp::TokenList &output, const simplecpp::TokenL
33413341
continue;
33423342
const std::string lhs(macrostr.substr(0,eq));
33433343
const std::string rhs(eq==std::string::npos ? std::string("1") : macrostr.substr(eq+1));
3344-
const Macro macro(lhs, rhs, dummy);
3345-
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
3344+
try {
3345+
const Macro macro(lhs, rhs, dummy);
3346+
macros.insert(std::pair<TokenString,Macro>(macro.name(), macro));
3347+
} catch (const std::runtime_error& e) {
3348+
if (outputList) {
3349+
simplecpp::Output err = {
3350+
Output::DUI_ERROR,
3351+
{},
3352+
e.what()
3353+
};
3354+
outputList->push_back(std::move(err));
3355+
}
3356+
output.clear();
3357+
return;
3358+
}
33463359
}
33473360

33483361
const bool strictAnsiUndefined = dui.undefined.find("__STRICT_ANSI__") != dui.undefined.cend();

test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3442,6 +3442,18 @@ static void tokenlist_api()
34423442
#endif // __cpp_lib_span
34433443
}
34443444

3445+
static void bad_macro_syntax() // #616
3446+
{
3447+
simplecpp::DUI dui;
3448+
dui.defines.emplace_back("\"");
3449+
3450+
simplecpp::OutputList outputList;
3451+
ASSERT_EQUALS("", preprocess("", dui, &outputList));
3452+
ASSERT_EQUALS(1, outputList.size());
3453+
ASSERT_EQUALS(simplecpp::Output::Type::DUI_ERROR, outputList.cbegin()->type);
3454+
ASSERT_EQUALS("bad macro syntax. macroname=\" value=1", outputList.cbegin()->msg);
3455+
}
3456+
34453457
static void isAbsolutePath() {
34463458
#ifdef _WIN32
34473459
ASSERT_EQUALS(true, simplecpp::isAbsolutePath("C:\\foo\\bar"));
@@ -3769,6 +3781,8 @@ int main(int argc, char **argv)
37693781

37703782
TEST_CASE(isAbsolutePath);
37713783

3784+
TEST_CASE(bad_macro_syntax);
3785+
37723786
TEST_CASE(fuzz_crash);
37733787

37743788
TEST_CASE(leak);

0 commit comments

Comments
 (0)