Bug in FAST variants of memset64() and memcpy64()

Issue #1995 closed
Lukáš Chmela created an issue


I've found a bug in void FAST memset64() and void FAST memcpy64(), file /src/stdio.c, lines 114 and 138 shouldn't be there (the incrementations of i).

If dest is not aligned, then the pointer is rounded up and the corresponding number of bytes subtracted from length n. If, furthermore, i is then incremented, you lose one iteration of the cycle right after (consider dest = 0x00 00 00 00 00 00 00 0E and n = 9).

Comments (6)

  1. Lukáš Chmela reporter

    Ups, sorry, the codes are wrong anyway. The subtraction from n must happen before dest (or srce respectively) is updated. So it should be like this:

    if ((intptr_t)dest & 7)
        n -= 8 - ((intptr_t)dest & 7);
        dest = (void*)((intptr_t)dest & ~7) + 8;

    for the memset64() and the same for memcpy64() with dest renamed to srce.

  2. Log in to comment