Skip to content

Keep indexed characters, bits and bytes of constants read-only - #1224

Merged
schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:const-index-read-only
Nov 23, 2026
Merged

schungx merged 1 commit into
rhaiscript:mainfrom
Rockyrok77:const-index-read-only

Conversation

@Rockyrok77

Copy link
Copy Markdown
Contributor

A constant pushed by the host can be changed by a script through an index on a string, integer or BLOB:

let mut scope = Scope::new();
scope.push_constant("s", "hello");
scope.push_constant("n", 42 as INT);

engine.run_with_scope(&mut scope, "s = \"jello\"");   // Err: Cannot modify constant s
engine.run_with_scope(&mut scope, "s[0] = 'j'");      // Ok(()), scope now holds s = "jello"
engine.run_with_scope(&mut scope, "n[0..4] = 15");    // Ok(()), n = 47

The same works for s[1..3] = "xy", n[0] = true, b[0] += 1 on a BLOB, and a string nested in a constant map or array (m.s[0] = 'z'). s[0].make_upper(), s[0..2].clear() and n[0..4].set_bit(0, true) change the constant too, and those also get through Rhai Grain. m.t = 1 adds a new property to a constant map. Constant arrays and maps are already protected, A[0] = 1 and C.x = 1 fail, so this hits only the index forms that go through a copy.

The cause is in Engine::get_indexed_mut. For a character, slice, bit or byte it returns a Target holding a fresh read-write copy of the item, and propagate_changed_value writes that copy back into the source. The constant check in eval_op_assignment and the non-pure method check in exec_native_fn_call both look at the copy, so both pass. Array and map elements don't have this problem because set_access_mode marks them read-only along with their container. The copy now gets the same treatment when the source is read-only, so these cases fail with ErrorAssignmentToConstant or ErrorNonPureMethodCallOnConstant, like arrays and the Grain VM do. For a missing key, the map arm inserted it into a read-only map before any check ran. It no longer inserts and returns a read-only () instead. Reading from a constant, or copying one into a variable and changing the copy, works as before.

test_constant_scope_index in tests/constants.rs fails on main at the first assertion and passes with the fix. cargo test --tests passes with default features and with the testing-environ CI rows for full features, no_index, no_object, no_optimize, unchecked, sync, only_i32, no_closure, no_float,only_i32,no_object,no_index,no_module,no_function,sync,grain and the sync,no_time,...,unchecked,debugging,grain row. cargo fmt --check and the CI clippy invocation are clean, and the no_ast,decimal,metadata,sync,internals,grain build compiles. The CHANGELOG line is under 1.27.1's bug fixes.

@schungx
schungx merged commit be7a598 into rhaiscript:main Nov 23, 2026
56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants