Skip to content

Commit 5310bad

Browse files
committed
Limit nesting depth in #if expressions
Deeply nested expressions overflowed the stack in the recursive evaluator. Report an error when the nesting is deeper than 256.
1 parent 79d0f4e commit 5310bad

2 files changed

Lines changed: 59 additions & 10 deletions

File tree

‎simplecpp.cpp‎

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,27 @@ namespace {
10801080

10811081
private:
10821082
const simplecpp::Token *mTok;
1083+
int mDepth{};
1084+
1085+
/**
1086+
* Limits the nesting depth, so deeply nested expressions can't overflow the stack.
1087+
* 256 is the minimum nesting of parenthesized expressions in C++ (Annex B).
1088+
*/
1089+
class DepthGuard {
1090+
public:
1091+
explicit DepthGuard(int &depth) : mCounter(depth) {
1092+
if (mCounter >= 256)
1093+
throw std::runtime_error("expression is nested too deeply");
1094+
++mCounter;
1095+
}
1096+
~DepthGuard() {
1097+
--mCounter;
1098+
}
1099+
DepthGuard(const DepthGuard &) = delete;
1100+
DepthGuard &operator=(const DepthGuard &) = delete;
1101+
private:
1102+
int &mCounter;
1103+
};
10831104

10841105
/** current operator, with alternative tokens ("and", "not_eq", ..) replaced */
10851106
const std::string &peek() const {
@@ -1114,6 +1135,7 @@ namespace {
11141135
const long long cond = logicalOr(eval);
11151136
if (!accept("?"))
11161137
return cond;
1138+
const DepthGuard guard(mDepth);
11171139
try {
11181140
const long long result1 = conditional(eval && cond != 0);
11191141
expect(":");
@@ -1245,21 +1267,24 @@ namespace {
12451267
}
12461268

12471269
long long unary(bool eval) {
1248-
if (accept("+"))
1249-
return unary(eval);
1250-
if (accept("-")) {
1251-
const long long value = unary(eval);
1270+
const std::string op = peek();
1271+
if (op != "+" && op != "-" && op != "!" && op != "~")
1272+
return primary(eval);
1273+
mTok = mTok->next;
1274+
const DepthGuard guard(mDepth);
1275+
const long long value = unary(eval);
1276+
if (op == "+")
1277+
return value;
1278+
if (op == "-")
12521279
return eval ? -value : 0;
1253-
}
1254-
if (accept("!"))
1255-
return unary(eval) == 0 ? 1 : 0;
1256-
if (accept("~"))
1257-
return ~unary(eval);
1258-
return primary(eval);
1280+
if (op == "!")
1281+
return value == 0 ? 1 : 0;
1282+
return ~value;
12591283
}
12601284

12611285
long long primary(bool eval) {
12621286
if (accept("(")) {
1287+
const DepthGuard guard(mDepth);
12631288
const long long result = conditional(eval);
12641289
expect(")");
12651290
return result;

‎test.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,6 +2797,29 @@ static void ifShortCircuit()
27972797
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, division/modulo by zero\n", toString(outputList));
27982798
}
27992799

2800+
static void ifNestingDepth()
2801+
{
2802+
const std::string ok = "#if " + std::string(256, '(') + "1" + std::string(256, ')') + "\n"
2803+
"1\n"
2804+
"#endif\n";
2805+
ASSERT_EQUALS("\n1", preprocess(ok.c_str()));
2806+
2807+
// deeply nested expressions must not overflow the stack
2808+
const char * const exprs[] = { "(", "!", "0?0:" };
2809+
for (const char * const expr : exprs) {
2810+
std::string code = "#if ";
2811+
for (int i = 0; i < 100000; ++i)
2812+
code += expr;
2813+
code += "1";
2814+
if (expr[0] == '(')
2815+
code += std::string(100000, ')');
2816+
code += "\n#endif\n";
2817+
simplecpp::OutputList outputList;
2818+
ASSERT_EQUALS("", preprocess(code.c_str(), &outputList));
2819+
ASSERT_EQUALS("file0,1,syntax_error,failed to evaluate #if condition, expression is nested too deeply\n", toString(outputList));
2820+
}
2821+
}
2822+
28002823
static void ifTrueFalse()
28012824
{
28022825
const char code[] = "#if true && !false\n"
@@ -5030,6 +5053,7 @@ static void runTests(int argc, char **argv, Input input)
50305053
TEST_CASE(ifoverflow);
50315054
TEST_CASE(ifdiv0);
50325055
TEST_CASE(ifShortCircuit);
5056+
TEST_CASE(ifNestingDepth);
50335057
TEST_CASE(ifTrueFalse);
50345058
TEST_CASE(ifalt); // using "and", "or", etc
50355059
TEST_CASE(ifexpr);

0 commit comments

Comments
 (0)