Thanks to visit codestin.com
Credit goes to github.com

Skip to content

-Wdangling-gsl incorrectly warns on return by value of std::string::iterator() -= n #48686

@miscco

Description

@miscco
Bugzilla Link 49342
Resolution FIXED
Resolved on Feb 27, 2021 11:05
Version trunk
OS Windows NT
Attachments reproducer
CC @CaseyCarter,@mgehre,@mnatsuhara,@zygoloid,@Xazax-hun

Extended Description

This bug has been encountered when testing MSVC STL shiny new constexpr string implementation with clang 12.x. A full reproducer is attached.

The code in question is:

#include <string>

constexpr bool test() {
    auto meow = "Hiss";
    [[maybe_unused]] auto it = std::string::const_iterator{meow + 4, nullptr} - 2;
    return true;
}

int main() {
    static_assert(test());
}

This explicitly creates a string iterator and call operator-. Now clangs gives the following error:

C:\STL\stl\inc\xstring(1964,16): warning: address of stack memory associated with local variable '_Tmp' returned
      [-Wreturn-stack-address]
        return _Tmp -= _Off;

This stems from the conventional implementation of operator+=

    _CONSTEX#20 _CONTAINER _String_const_iterator& operator+=(const difference_type _Off) noexcept {
#if _ITERATOR_DEBUG_LEVEL >= 1
        _Verify_offset(_Off);
#endif // _ITERATOR_DEBUG_LEVEL >= 1
        _Ptr += _Off;
        return *this;
    }

Here a reference is returned that seems to be mistaken of taking the address? The same problem happens with operator+=

Now the real fun part is, that I was fully unable to reproduce this in any meaningfull way. This is the furthest I came

#include <string>

constexpr bool test() {
    auto meow = "Hiss";
    [[maybe_unused]] auto it =
        std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>{meow + 4, nullptr} - 2;
    return true;
}

int main() {
    static_assert(test());
}

Even simply copying the content of _String_const_iterator into the file and using it instead of the real thing makes the bug go away. So there seems to be some highly unusual problem around

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++20

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions