Skip to content

Commit

Permalink
Merge pull request #1054 from 64/memmove
Browse files Browse the repository at this point in the history
options/internal: use memcpy when memmove arguments don't overlap
  • Loading branch information
64 authored Apr 15, 2024
2 parents f666985 + 21eed15 commit c8b7fd6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions options/internal/generic/essential.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,21 @@ void *memset(void *dest, int byte, size_t count) {
// --------------------------------------------------------------------------------------

void *memmove(void *dest, const void *src, size_t size) {
char *dest_bytes = (char *)dest;
char *src_bytes = (char *)src;
if(dest_bytes < src_bytes) {
// Use uintptr_t for pointer comparisons because otherwise it's undefined behaviour
// when dest and src point to different objects.
uintptr_t udest = reinterpret_cast<uintptr_t>(dest);
uintptr_t usrc = reinterpret_cast<uintptr_t>(src);

if(udest < usrc || usrc + size <= udest) {
return forward_copy(dest, src, size);
}else if(dest_bytes > src_bytes) {
} else if(udest > usrc) {
char *dest_bytes = (char *)dest;
char *src_bytes = (char *)src;

for(size_t i = 0; i < size; i++)
dest_bytes[size - i - 1] = src_bytes[size - i - 1];
}

return dest;
}

Expand Down

0 comments on commit c8b7fd6

Please sign in to comment.