Skip to content

Commit e6ebc2b

Browse files
authored
Don't continue parsing expressions after ternary operators. (#3847)
Further benefits: new code requires less checks. Fixes #3841 Fixes #3842 JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 1414e9b commit e6ebc2b

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

jerry-core/parser/js/js-parser-expr.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3304,13 +3304,9 @@ parser_parse_initializer_by_next_char (parser_context_t *context_p, /**< context
33043304

33053305
/**
33063306
* Process ternary expression.
3307-
*
3308-
* @return true - continue with primary expression parsing
3309-
* false - otherwise
33103307
*/
3311-
static bool
3312-
parser_process_ternary_expression (parser_context_t *context_p, /**< context */
3313-
size_t grouping_level) /**< grouping level */
3308+
static void
3309+
parser_process_ternary_expression (parser_context_t *context_p) /**< context */
33143310
{
33153311
JERRY_ASSERT (context_p->token.type == LEXER_QUESTION_MARK);
33163312

@@ -3354,7 +3350,6 @@ parser_process_ternary_expression (parser_context_t *context_p, /**< context */
33543350
parser_flush_cbc (context_p);
33553351

33563352
parser_process_binary_opcodes (context_p, 0);
3357-
return grouping_level >= PARSER_GROUPING_LEVEL_INCREASE;
33583353
} /* parser_process_ternary_expression */
33593354

33603355
/**
@@ -3524,12 +3519,6 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
35243519
continue;
35253520
}
35263521

3527-
if (JERRY_UNLIKELY (context_p->token.type == LEXER_QUESTION_MARK)
3528-
&& (grouping_level != PARSE_EXPR_LEFT_HAND_SIDE)
3529-
&& parser_process_ternary_expression (context_p, grouping_level))
3530-
{
3531-
continue;
3532-
}
35333522
break;
35343523
}
35353524

@@ -3538,19 +3527,29 @@ parser_parse_expression (parser_context_t *context_p, /**< context */
35383527
break;
35393528
}
35403529

3541-
if (JERRY_UNLIKELY (context_p->token.type == LEXER_COMMA)
3542-
&& (!(options & PARSE_EXPR_NO_COMMA) || grouping_level >= PARSER_GROUPING_LEVEL_INCREASE))
3530+
if (JERRY_UNLIKELY (context_p->token.type == LEXER_QUESTION_MARK))
35433531
{
3544-
parser_process_expression_sequence (context_p);
3545-
continue;
3546-
}
3532+
parser_process_ternary_expression (context_p);
35473533

3548-
if (LEXER_IS_BINARY_OP_TOKEN (context_p->token.type))
3534+
if (context_p->token.type == LEXER_RIGHT_PAREN)
3535+
{
3536+
goto process_unary_expression;
3537+
}
3538+
}
3539+
else if (LEXER_IS_BINARY_OP_TOKEN (context_p->token.type))
35493540
{
35503541
parser_append_binary_token (context_p);
35513542
lexer_next_token (context_p);
35523543
continue;
35533544
}
3545+
3546+
if (JERRY_UNLIKELY (context_p->token.type == LEXER_COMMA)
3547+
&& (!(options & PARSE_EXPR_NO_COMMA) || grouping_level >= PARSER_GROUPING_LEVEL_INCREASE))
3548+
{
3549+
parser_process_expression_sequence (context_p);
3550+
continue;
3551+
}
3552+
35543553
break;
35553554
}
35563555

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
eval('$?$:$=>{ }?{ }:$')
17+
assert(false)
18+
} catch (e) {
19+
assert(e instanceof SyntaxError)
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
try {
16+
eval('while(0 ? 0 : ()=>{} | {})')
17+
assert(false)
18+
} catch (e) {
19+
assert(e instanceof SyntaxError)
20+
}

0 commit comments

Comments
 (0)