Skip to content

Latest commit

 

History

History
57 lines (43 loc) · 1.99 KB

error-strncat-param-overlap.md

File metadata and controls

57 lines (43 loc) · 1.99 KB
title description ms.date f1_keywords helpviewer_keywords
Error: strncat-param-overlap
Source examples and live debug screenshots for strcat parameter overlap errors.
03/02/2021
strncat-param-overlap
strncat-param-overlap error
AddressSanitizer error strcat-param-overlap

Error: strncat-param-overlap

Address Sanitizer Error: strncat-param-overlap

Code that moves memory in overlapping buffer can cause hard-to-diagnose errors.

Example

This example shows how AddressSanitizer can catch errors caused by overlapped parameters to CRT functions.

(Based on llvm-project/compiler-rt/test/asan/TestCases/strncat-overlap.cpp.)

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

void bad_function() {
    char buffer[] = "hello\0XXX";
    strncat(buffer, buffer + 1, 3); // BOOM
    return;
}

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
devenv /debugexe example1.exe

Resulting error

:::image type="content" source="media/strcat-param-overlap-example-1.png" alt-text="Screenshot of debugger displaying strncat-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