Skip to content

<string>: Potentially missing terminator when ASAN is enabled. #3188

Description

@W4RH4WK

I am not 100% certain about this one, but while debugging one of our tools (with address sanitizer enabled) I discovered an std::string with a non-null-terminated payload. This is problematic as c_str() will just return a pointer to the internal buffer:

STL/stl/inc/xstring

Lines 4254 to 4256 in c873cf0

_NODISCARD _CONSTEXPR20 _Ret_z_ const _Elem* c_str() const noexcept {
return _Mypair._Myval2._Myptr();
}

STL/stl/inc/xstring

Lines 2290 to 2297 in c873cf0

_CONSTEXPR20 const value_type* _Myptr() const noexcept {
const value_type* _Result = _Bx._Buf;
if (_Large_string_engaged()) {
_Result = _Unfancy(_Bx._Ptr);
}
return _Result;
}

Tracking down the construction, the problematic code is here:

STL/stl/inc/xstring

Lines 2754 to 2758 in c873cf0

#ifdef _INSERT_STRING_ANNOTATION
_Traits::move(_My_data._Bx._Buf, _Arg, _Count);
#else // ^^^ _INSERT_STRING_ANNOTATION ^^^ // vvv !_INSERT_STRING_ANNOTATION vvv
_Traits::move(_My_data._Bx._Buf, _Arg, _BUF_SIZE);
#endif // !_INSERT_STRING_ANNOTATION

Instead of moving the whole buffer (including the terminator) from the source, this moves only _Count characters, which does not include the terminator. Subsequent code does/did not add the required terminator.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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