Skip to content

Fix direct call to eval from strict mode code #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion jerry-core/ecma/operations/ecma-eval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ ecma_op_eval_chars_buffer (const ecma_char_t *code_p, /**< code characters buffe
const opcode_t *opcodes_p;
bool is_syntax_correct;

bool is_strict_call = (is_direct && is_called_from_strict_mode_code);

is_syntax_correct = parser_parse_eval ((const char *) code_p,
code_buffer_size,
is_strict_call,
&opcodes_p);

if (!is_syntax_correct)
Expand All @@ -108,7 +111,7 @@ ecma_op_eval_chars_buffer (const ecma_char_t *code_p, /**< code characters buffe
is_strict_prologue = true;
}

bool is_strict = (is_strict_prologue || (is_direct && is_called_from_strict_mode_code));
bool is_strict = (is_strict_call || is_strict_prologue);

ecma_value_t this_binding;
ecma_object_t *lex_env_p;
Expand Down
16 changes: 13 additions & 3 deletions jerry-core/parser/js/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3062,6 +3062,8 @@ parser_parse_program (const char *source_p, /**< source code buffer */
size_t source_size, /**< source code size in bytes */
bool in_function, /**< flag indicating if we are parsing body of a function */
bool in_eval, /**< flag indicating if we are parsing body of eval code */
bool is_strict, /**< flag, indicating whether current code
* inherited strict mode from code of an outer scope */
const opcode_t **out_opcodes_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
{
Expand All @@ -3088,6 +3090,7 @@ parser_parse_program (const char *source_p, /**< source code buffer */
STACK_INIT (scopes);
STACK_PUSH (scopes, scopes_tree_init (NULL));
serializer_set_scope (STACK_TOP (scopes));
scopes_tree_set_strict_mode (STACK_TOP (scopes), is_strict);
lexer_set_strict_mode (scopes_tree_strict_mode (STACK_TOP (scopes)));

jmp_buf *syntax_error_label_p = syntax_get_syntax_error_longjmp_label ();
Expand Down Expand Up @@ -3165,7 +3168,7 @@ parser_parse_script (const char *source, /**< source script */
const opcode_t **opcodes_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
{
return parser_parse_program (source, source_size, false, false, opcodes_p);
return parser_parse_program (source, source_size, false, false, false, opcodes_p);
} /* parser_parse_script */

/**
Expand All @@ -3177,10 +3180,12 @@ parser_parse_script (const char *source, /**< source script */
bool
parser_parse_eval (const char *source, /**< string passed to eval() */
size_t source_size, /**< string size in bytes */
bool is_strict, /**< flag, indicating whether eval is called
* from strict code in direct mode */
const opcode_t **opcodes_p) /**< out: generated byte-code array
* (in case there were no syntax errors) */
{
return parser_parse_program (source, source_size, false, true, opcodes_p);
return parser_parse_program (source, source_size, false, true, is_strict, opcodes_p);
} /* parser_parse_eval */

/**
Expand All @@ -3206,7 +3211,12 @@ parser_parse_new_function (const char **params, /**< array of arguments of new F
FIXME ("check parameter's name for syntax errors");
lit_find_or_create_literal_from_charset ((ecma_char_t *) params[i], (ecma_length_t) strlen (params[i]));
}
return parser_parse_program (params[params_count - 1], strlen (params[params_count - 1]), true, false, out_opcodes_p);
return parser_parse_program (params[params_count - 1],
strlen (params[params_count - 1]),
true,
false,
false,
out_opcodes_p);
} /* parser_parse_new_function */

/**
Expand Down
2 changes: 1 addition & 1 deletion jerry-core/parser/js/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

void parser_set_show_opcodes (bool);
bool parser_parse_script (const char *, size_t, const opcode_t **);
bool parser_parse_eval (const char *, size_t, const opcode_t **);
bool parser_parse_eval (const char *, size_t, bool, const opcode_t **);
bool parser_parse_new_function (const char **, size_t, const opcode_t **);

#endif /* PARSER_H */
10 changes: 10 additions & 0 deletions tests/jerry/eval.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function f2 (global)
assert (v2 === 'local value');
assert (typeof (global.v2) === 'undefined');
assert (r === undefined);

try
{
eval ('arguments = 1;');
assert (false);
}
catch (e)
{
assert (e instanceof SyntaxError);
}
}

f2 (this);
Expand Down