Skip to content

Commit a48a341

Browse files
committed
fix non-standard memcpy
the movsb-family of instructions modifies the destination pointer, pass them a local copy of it instead so the returned dest pointer stays intact.
1 parent ebfadf6 commit a48a341

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/memcpy.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,29 @@ void* memset(void* dest, char val, uint64_t len)
99

1010
static inline void* movsb(void* dst, const void* src, size_t size)
1111
{
12-
__asm__ volatile("rep movsb" : "+D"(dst), "+S"(src), "+c"(size) : : "memory");
12+
void *copy = dst;
13+
__asm__ volatile("rep movsb" : "+D"(copy), "+S"(src), "+c"(size) : : "memory");
1314
return dst;
1415
}
1516

1617
static inline void* movsl(void* dst, const void* src, size_t size)
1718
{
18-
__asm__ volatile("rep movsl" : "+D"(dst), "+S"(src), "+c"(size) : : "memory");
19+
void *copy = dst;
20+
__asm__ volatile("rep movsl" : "+D"(copy), "+S"(src), "+c"(size) : : "memory");
1921
return dst;
2022
}
2123

2224
static inline void* movsw(void* dst, const void* src, size_t size)
2325
{
24-
__asm__ volatile("rep movsw" : "+D"(dst), "+S"(src), "+c"(size) : : "memory");
26+
void *copy = dst;
27+
__asm__ volatile("rep movsw" : "+D"(copy), "+S"(src), "+c"(size) : : "memory");
2528
return dst;
2629
}
2730

2831
static inline void* movsq(void* dst, const void* src, size_t size)
2932
{
30-
__asm__ volatile("rep movsq" : "+D"(dst), "+S"(src), "+c"(size) : : "memory");
33+
void *copy = dst;
34+
__asm__ volatile("rep movsq" : "+D"(copy), "+S"(src), "+c"(size) : : "memory");
3135
return dst;
3236
}
3337

0 commit comments

Comments
 (0)