fix pointer aliasing in in_parallel.rs #1115
Merged
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.
In
in_parallel_with_slice()
, there's a&mut *input.0
that creates a&mut [I]
of the whole slice inInput
. The same value is created in multiple threads, thus potentially resulting in multiple threads having mutable references to the same value.This patch fixes that by instead only getting mutable pointers to individual values. It does so by storing a
*mut T
inInput
instead of the current*mut [I]
, and then usingpointer::add()
to get pointers to individual elements.Thanks to @Ralith for bringing this up and proposing the solution in our review of unsafe code at Google.