Force structures to be Copy instead of Clone #2
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.
I fixed a bug related to unsafe code inside the
RdxSort::rdxsort
slice implementation where you used the unsafecopy_nonoverlapping
function onClone
restricted types, which is not enough to be safe.rdxsort-rs/src/sort.rs
Lines 83 to 87 in c654971
As the function documentation warns us:
As a good example we can just take reference counted types that must increment a counter when cloned and decrement it when dropped, in this case you just use
memcpy
internally on theRc
structure that does not inform the type that it has been cloned but before that you usediter().cloned()
on these same structs.rdxsort-rs/src/sort.rs
Line 51 in c654971
It is not safe because it could generate use-after-free bugs, the
Rc
is previously cloned, incrementing the internal counter, copied without incrementing it (the pointer to the data is copied without being tracked by the counter) and dropped so decrementing the counter which can trigger the drop function, the memory area where the copy has been made can contain pointers to freed data.So solutions to fix this bug could be either:
T
type to beCopy
therefore this function call can be considered "safe"Vec::set_len
)