Edit

Share via


Error: memcpy-param-overlap

Address Sanitizer Error: memcpy-param-overlap

Note

The /Oi flag is required to reliably detect memcpy-param-overlap errors. This flag tells the compiler to treat memcpy and other functions as intrinsics, which is necessary because some versions of the standard library implement them as such. Since ASan is a dynamic analysis tool, it can only detect errors with an observable runtime effect. Note that when /O2 is also set, ASan may not be able to reliably detect memcpy-param-overlap errors because the intrinsic variant of these functions isn't guaranteed to be used. For more information, see /Oi docs.

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

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