Edit

Share via


Error: memcpy-param-overlap

Address Sanitizer Error: memcpy-param-overlap

The CRT function memcpy doesn't support overlapping memory. The CRT provides an alternative to memcpy that does support overlapping memory: memmove.

A common error is to treat memmove as being semantically equivalent to memcpy.

Example

// example1.cpp
// memcpy-param-overlap error
#include <string.h>

__declspec(noinline) void bad_function() {
    char buffer[] = "hello";

    memcpy(buffer, buffer + 1, 5); // BOOM!
}

int main(int argc, char **argv) {
    bad_function();
    return 0;
}

To build and test this example, run these commands in a Visual Studio 2019 version 16.9 or later developer command prompt:

cl example1.cpp /fsanitize=address /Zi /Oi
devenv /debugexe example1.exe

The /Oi flag tells the compiler to treat memcpy and memmove as intrinsic functions. This is necessary because some versions of the standard library implement memcpy and memmove in the same way. Because ASAN is a dynamic analysis tool, it only detects errors with an observable runtime effect.

Resulting error

Screenshot of debugger displaying memcpy-param-overlap error in example 1.

See also

AddressSanitizer overview
AddressSanitizer known issues
AddressSanitizer build and language reference
AddressSanitizer runtime reference
AddressSanitizer shadow bytes
AddressSanitizer cloud or distributed testing
AddressSanitizer debugger integration
AddressSanitizer error examples