Skip to content

Commit 7d5a885

Browse files
authored
fix(memmove): move all bytes
Previously, when trying to move a region "backwards" with a length that wasn't a multiple of the word size, we'd move the first bytes twice (first byte-by-byte, then as part of the last word), and we'd fail to move `m` bytes from the very end. Now, when trying to move 15 bytes to `add0` we will write to `add[2,1,0]` (or `add[0,1,2]`), then bump the pointer to `add3` where we'll write `add[f-c, b-8, 7-3]` (or `addr[3-7, 8-b, c-f]`) a word at a time. Demonstrating, once again, that it's better to run some examples when writing this kind of code: by hand, if necessary. I'm not sure how I convinced myself the pointer shouldn't advance when we're copying backwards, but there it was.
1 parent 356fbdf commit 7d5a885

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,16 @@ mod mem {
123123
// If src is less than dst, just copy from the end.
124124
// If src is greater than dst, just copy from the beginning."
125125
// — https://stackoverflow.com/a/3572519/151464
126-
let last;
127126
for i in if src < dest as *const u8 {
128-
last = 0;
129127
Idx::Backward(m)
130128
} else {
131-
last = m;
132129
Idx::Forward(0, m)
133130
} {
134131
*dest.add(i) = *src.add(i);
135132
}
136133

137-
let dest = dest.add(last).cast::<usize>();
138-
let src = src.add(last).cast::<usize>();
134+
let dest = dest.add(m).cast::<usize>();
135+
let src = src.add(m).cast::<usize>();
139136

140137
for i in if src < dest as *const usize {
141138
Idx::Backward(n)

0 commit comments

Comments
 (0)