diff --git a/CHANGELOG.md b/CHANGELOG.md index c9071d0d3..267470b77 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Bug fixes * Fixed undefined behavior (an aliasing violation detected by Miri) when a variable is passed to a function that does not take its first argument by `&mut`, e.g. `x.contains("a")` or `f(x)` for a script function. This was a regression in 1.27.0. * `sort` and `sort_desc` no longer panic on an array of floating-point numbers that contains NaN. NaN now sorts after every other number (before them for `sort_desc`) (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)). * `pad` on an array now enforces `max_array_size` when the padding length is large enough to overflow the size calculation. Previously it could pass the check and then panic or abort the process while allocating (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)). +* `eval` is no longer allowed in `Engine::eval_expression` and `Engine::compile_expression`. Previously it could run statements, loops and assignments inside an expression, including assignments to variables in the `Scope` passed in (thanks [`@Rockyrok77`](https://github.com/Rockyrok77)). Enhancements ------------ diff --git a/src/parser.rs b/src/parser.rs index a3b079d8a..c1d08ccf6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -618,6 +618,21 @@ impl Engine { capture_parent_scope: bool, #[cfg(not(feature = "no_module"))] mut namespace: crate::module::Namespace, ) -> ParseResult { + // `eval` runs a full script in the current scope, so it cannot be allowed + // where statements are not + #[cfg(not(feature = "no_module"))] + let is_eval = namespace.is_empty() && id == crate::engine::KEYWORD_EVAL; + #[cfg(feature = "no_module")] + let is_eval = id == crate::engine::KEYWORD_EVAL; + + if is_eval && settings.has_flag(ParseSettingFlags::DISALLOW_STATEMENTS_IN_BLOCKS) { + return Err(LexError::ImproperSymbol( + id.to_string(), + "'eval' cannot be used in an expression".into(), + ) + .into_err(settings.pos)); + } + let (token, token_pos) = if no_args { &(Token::RightParen, Position::NONE) } else { diff --git a/tests/expressions.rs b/tests/expressions.rs index 4e0505772..6e2566695 100644 --- a/tests/expressions.rs +++ b/tests/expressions.rs @@ -59,8 +59,15 @@ fn test_expressions() { let _ = engine.compile_expression("x = 42").unwrap_err(); let _ = engine.compile_expression("let x = 42").unwrap_err(); let _ = engine.compile_expression("do { break 42; } while true").unwrap_err(); + let _ = engine.compile_expression(r#"eval("let x = 42; x")"#).unwrap_err(); + let _ = engine.eval_expression_with_scope::<()>(&mut scope, r#"eval("x = 42")"#).unwrap_err(); + assert_eq!(scope.get_value::("x").unwrap(), 10); + #[cfg(not(feature = "no_index"))] + #[cfg(not(feature = "no_function"))] + let _ = engine.compile_expression(r#"[1].map(|v| eval("v"))"#).unwrap_err(); engine.compile("40 + { let x = 2; x }").unwrap(); + engine.compile(r#"eval("let x = 42; x")"#).unwrap(); } /// This example taken from https://github.com/rhaiscript/rhai/issues/115