diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2015-11-07 18:09:20 +0100 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2015-11-07 18:09:20 +0100 |
| commit | 0f5e30d16004334cac1a25dbeefc0cfb886a404d (patch) | |
| tree | 3809175f39543f1fb6000fb9f4418323c160941f | |
| parent | 35fd1bab5e727061248c7810ca1fbe81e336d019 (diff) | |
| download | rust-0f5e30d16004334cac1a25dbeefc0cfb886a404d.tar.gz rust-0f5e30d16004334cac1a25dbeefc0cfb886a404d.zip | |
sort: Guard the fast path by length check
The right part must not be empty.
| -rw-r--r-- | src/libcollections/slice.rs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index cb3f39e0cac..9ea3389e81c 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -1066,10 +1066,12 @@ fn merge_sort<T, F>(v: &mut [T], mut compare: F) where F: FnMut(&T, &T) -> Order let mut out = buf_tmp.offset(start as isize); let out_end = buf_tmp.offset(right_end_idx as isize); - // if left[last] <= right[0], they are already in order: + // If left[last] <= right[0], they are already in order: // fast-forward the left side (the right side is handled // in the loop). - if compare(&*right.offset(-1), &*right) != Greater { + // If `right` is not empty then left is not empty, and + // the offsets are in bounds. + if right != right_end && compare(&*right.offset(-1), &*right) != Greater { let elems = (right_start as usize - left as usize) / mem::size_of::<T>(); ptr::copy_nonoverlapping(&*left, out, elems); out = out.offset(elems as isize); |
