Repository navigation
Keep indexed characters, bits and bytes of constants read-only - #1224
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A constant pushed by the host can be changed by a script through an index on a string, integer or BLOB:
The same works for
s[1..3] = "xy",n[0] = true,b[0] += 1on a BLOB, and a string nested in a constant map or array (m.s[0] = 'z').s[0].make_upper(),s[0..2].clear()andn[0..4].set_bit(0, true)change the constant too, and those also get through Rhai Grain.m.t = 1adds a new property to a constant map. Constant arrays and maps are already protected,A[0] = 1andC.x = 1fail, 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 aTargetholding a fresh read-write copy of the item, andpropagate_changed_valuewrites that copy back into the source. The constant check ineval_op_assignmentand the non-pure method check inexec_native_fn_callboth look at the copy, so both pass. Array and map elements don't have this problem becauseset_access_modemarks them read-only along with their container. The copy now gets the same treatment when the source is read-only, so these cases fail withErrorAssignmentToConstantorErrorNonPureMethodCallOnConstant, 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_indexintests/constants.rsfails onmainat the first assertion and passes with the fix.cargo test --testspasses with default features and with thetesting-environCI 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,grainand thesync,no_time,...,unchecked,debugging,grainrow.cargo fmt --checkand the CI clippy invocation are clean, and theno_ast,decimal,metadata,sync,internals,grainbuild compiles. The CHANGELOG line is under 1.27.1's bug fixes.