Skip to content

Commit 653dab3

Browse files
committed
fix: address pr comments
Addresses the following issues raised in the review process: - Move MAX_PARSER_DEPTH to an anomymous namespace. - Remove default value for current depth to prevent accidentally bypassing the depth protection. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
1 parent 0c4fc3c commit 653dab3

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

core/parser.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ std::string jsonnet_unparse_number(double v)
5252
return ss.str();
5353
}
5454

55+
namespace {
56+
57+
static const Fodder EMPTY_FODDER;
58+
5559
/** Maximum parsing depth to avoid stack overflow due to pathological or malicious code.
5660
* This is especially important when parsing deeply nested structures that could lead to
5761
* excessive recursion in the parser functions.
5862
*/
5963
static const unsigned MAX_PARSER_DEPTH = 1000;
6064

61-
namespace {
62-
63-
static const Fodder EMPTY_FODDER;
64-
6565
static bool op_is_unary(const std::string &op, UnaryOp &uop)
6666
{
6767
auto it = unary_map.find(op);
@@ -163,7 +163,7 @@ class Parser {
163163
* \param current_depth Current recursion depth to prevent stack overflow.
164164
* \returns The last token (the one that matched parameter end).
165165
*/
166-
Token parseArgs(ArgParams &args, const std::string &element_kind, bool &got_comma, unsigned current_depth = 0)
166+
Token parseArgs(ArgParams &args, const std::string &element_kind, bool &got_comma, unsigned current_depth)
167167
{
168168
got_comma = false;
169169
bool first = true;
@@ -214,7 +214,7 @@ class Parser {
214214
* \param current_depth Current recursion depth to prevent stack overflow.
215215
* \returns The parameters as ArgParams.
216216
*/
217-
ArgParams parseParams(const std::string &element_kind, bool &got_comma, Fodder &close_fodder, unsigned current_depth = 0)
217+
ArgParams parseParams(const std::string &element_kind, bool &got_comma, Fodder &close_fodder, unsigned current_depth)
218218
{
219219
ArgParams params;
220220
Token paren_r = parseArgs(params, element_kind, got_comma, current_depth);
@@ -244,7 +244,7 @@ class Parser {
244244
* \param current_depth Current recursion depth to prevent stack overflow.
245245
* \returns The token after the binding (comma or semicolon).
246246
*/
247-
Token parseBind(Local::Binds &binds, unsigned current_depth = 0)
247+
Token parseBind(Local::Binds &binds, unsigned current_depth)
248248
{
249249
Token var_id = popExpect(Token::IDENTIFIER);
250250
auto *id = alloc->makeIdentifier(var_id.data32());
@@ -285,7 +285,7 @@ class Parser {
285285
* \param current_depth Current recursion depth to prevent stack overflow.
286286
* \returns The closing brace token.
287287
*/
288-
Token parseObjectRemainder(AST *&obj, const Token &tok, unsigned current_depth = 0)
288+
Token parseObjectRemainder(AST *&obj, const Token &tok, unsigned current_depth)
289289
{
290290
if (current_depth >= MAX_PARSER_DEPTH) {
291291
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");
@@ -584,7 +584,7 @@ class Parser {
584584
*/
585585
Token parseComprehensionSpecs(Token::Kind end, Fodder for_fodder,
586586
std::vector<ComprehensionSpec> &specs,
587-
unsigned current_depth = 0)
587+
unsigned current_depth)
588588
{
589589
if (current_depth >= MAX_PARSER_DEPTH) {
590590
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");
@@ -623,7 +623,7 @@ class Parser {
623623
* \param current_depth Current recursion depth to prevent stack overflow.
624624
* \returns The parsed AST.
625625
*/
626-
AST *parseTerminalBracketsOrUnary(unsigned current_depth = 0)
626+
AST *parseTerminalBracketsOrUnary(unsigned current_depth)
627627
{
628628
if (current_depth >= MAX_PARSER_DEPTH) {
629629
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");
@@ -807,7 +807,7 @@ class Parser {
807807
* \param current_depth Current recursion depth to prevent stack overflow.
808808
* \returns The parsed AST or nullptr.
809809
*/
810-
AST *maybeParseGreedy(unsigned current_depth = 0)
810+
AST *maybeParseGreedy(unsigned current_depth)
811811
{
812812
if (current_depth >= MAX_PARSER_DEPTH) {
813813
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");
@@ -976,7 +976,7 @@ class Parser {
976976
* \param current_depth Current recursion depth to prevent stack overflow.
977977
* \returns The parsed AST.
978978
*/
979-
AST *parse(unsigned max_precedence, unsigned current_depth = 0)
979+
AST *parse(unsigned max_precedence, unsigned current_depth)
980980
{
981981
if (current_depth >= MAX_PARSER_DEPTH) {
982982
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");
@@ -1005,7 +1005,7 @@ class Parser {
10051005
* \param current_depth Current recursion depth to prevent stack overflow.
10061006
* \returns The parsed AST.
10071007
*/
1008-
AST *parseInfix(AST *lhs, const Token &begin, unsigned max_precedence, unsigned current_depth = 0)
1008+
AST *parseInfix(AST *lhs, const Token &begin, unsigned max_precedence, unsigned current_depth)
10091009
{
10101010
if (current_depth >= MAX_PARSER_DEPTH) {
10111011
throw StaticError(peek().location, "Exceeded maximum parse depth limit.");

0 commit comments

Comments
 (0)