|
| 1 | +--- |
| 2 | +title: "Error: container-overflow" |
| 3 | +description: "Source examples and live debug screenshots for container overflow errors." |
| 4 | +ms.date: 04/15/2022 |
| 5 | +f1_keywords: ["container-overflow", "mismatch detected for 'annotate_vector'", "_DISABLE_VECTOR_ANNOTATION"] |
| 6 | +helpviewer_keywords: ["container-overflow error", "AddressSanitizer error container-overflow", "mismatch detected for 'annotate_vector'", "_DISABLE_VECTOR_ANNOTATION"] |
| 7 | +--- |
| 8 | + |
| 9 | +# Error: `container-overflow` |
| 10 | + |
| 11 | +> Address Sanitizer Error: Container overflow |
| 12 | +
|
| 13 | +In Visual Studio 2022 version 17.2 and later, the MSVC standard library (STL) is partially enlightened to understand the AddressSanitizer. The following container types have inserted extra annotations to detect memory access issues: |
| 14 | + |
| 15 | +| Standard container type | Disable annotations macro | Supported in version | |
| 16 | +|--|--|--| |
| 17 | +| `std::vector` | `_DISABLE_VECTOR_ANNOTATION` | Visual Studio 2022 17.2 | |
| 18 | + |
| 19 | +When a standard type has annotations enabled, to avoid one-definition-rule (ODR) violations, each static library and object used to link the binary must also enable those annotations. Effectively, you must build those static libraries and objects with AddressSanitizer enabled. Mixing code with different annotation settings causes an error: |
| 20 | + |
| 21 | +```Output |
| 22 | +my_static.lib(my_code.obj) : error LNK2038: mismatch detected for 'annotate_vector': value '0' doesn't match value '1' in main.obj |
| 23 | +``` |
| 24 | + |
| 25 | +To resolve this error, either disable annotations in all projects that use the corresponding macro, or build each project by using **`/fsanitize=address`** and annotations enabled. (Annotations are enabled by default.) |
| 26 | + |
| 27 | +## Example: Access reserved memory in a `std::vector` |
| 28 | + |
| 29 | +```cpp |
| 30 | +// Compile with: cl /EHsc /fsanitize=address /Zi |
| 31 | +#include <vector> |
| 32 | + |
| 33 | +int main() { |
| 34 | + // Create a vector of size 10, but with a capacity of 20. |
| 35 | + std::vector<int> v(10); |
| 36 | + v.reserve(20); |
| 37 | + |
| 38 | + // In versions prior to 17.2, MSVC ASan does NOT raise an exception here. |
| 39 | + // While this is an out-of-bounds write to 'v', MSVC ASan |
| 40 | + // ensures the write is within the heap allocation size (20). |
| 41 | + // With 17.2 and later, MSVC ASan will raise a 'container-overflow' exception: |
| 42 | + // ==18364==ERROR: AddressSanitizer: container-overflow on address 0x1263cb8a0048 at pc 0x7ff6466411ab bp 0x005cf81ef7b0 sp 0x005cf81ef7b8 |
| 43 | + v[10] = 1; |
| 44 | + |
| 45 | + // Regardless of version, MSVC ASan DOES raise an exception here, as this write |
| 46 | + // is out of bounds from the heap allocation. |
| 47 | + v[20] = 1; |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +To build and test this example, run the following commands in a Visual Studio 2022 version 17.2 or later [Developer command prompt](../build/building-on-the-command-line.md#developer_command_prompt_shortcuts) window: |
| 52 | + |
| 53 | +```cmd |
| 54 | +cl /EHsc example1.cpp /fsanitize=address /Zi |
| 55 | +devenv /debugexe example1.exe |
| 56 | +``` |
| 57 | + |
| 58 | +### Error result of reserved memory access in a `std::vector` |
| 59 | + |
| 60 | +:::image type="content" source="media/container-overflow-example-1.png" alt-text="Screenshot of debugger displaying container-overflow error in example 1." lightbox="media/container-overflow-example-1.png"::: |
| 61 | + |
| 62 | +## See also |
| 63 | + |
| 64 | +[AddressSanitizer overview](./asan.md)\ |
| 65 | +[AddressSanitizer known issues](./asan-known-issues.md)\ |
| 66 | +[AddressSanitizer build and language reference](./asan-building.md)\ |
| 67 | +[AddressSanitizer runtime reference](./asan-runtime.md)\ |
| 68 | +[AddressSanitizer shadow bytes](./asan-shadow-bytes.md)\ |
| 69 | +[AddressSanitizer cloud or distributed testing](./asan-offline-crash-dumps.md)\ |
| 70 | +[AddressSanitizer debugger integration](./asan-debugger-integration.md)\ |
| 71 | +[AddressSanitizer error examples](./asan-error-examples.md) |
0 commit comments