Skip to content

<string>: ASan annotations do not prevent writing to allocated but uninitialized basic_string memory #5251

@davidmrdavid

Description

@davidmrdavid

Describe the bug

ASan does not fire when writing to uninitialized memory in a basic_string, unlike with vector.

Command-line test case

C:\Temp>type repro.cpp
#include <vector>
#include <string>

int main()
{
    // This crashes (expectedly)
    //std::vector<int> vec;
    //vec.reserve(100);
    //vec.data()[50] = 1;

    // This does not crash (it should crash, like `vector`)
    std::basic_string<char> myString;
    myString.reserve(100);
    char* data = &myString[0];
    data[50] = 'A';

}

C:\Temp>cl /EHsc /Zi /fsanitize=address .\repro.cpp

Microsoft (R) C/C++ Optimizing Compiler Version 19.43.34618 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.43.34618.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
/debug
/InferAsanLibs
repro.obj

C:\Temp>.\repro.exe
<no ASan failure thrown>

Expected behavior

ASan should throw a container-overflow-type error due to a WRITE on an uninitialized section of container memory.

STL version

Microsoft Visual Studio Enterprise 2022
Version 17.13.0 Preview 2.1

Additional context

This bug was discovered while developing the basic_string test case for #5241, and has already been discussed internally.

Metadata

Metadata

Assignees

Labels

ASanAddress SanitizerbugSomething isn't workingfixedSomething works now, yay!

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions