[llvm-bugs] [Bug 49342] New: Clang incorrectly rejects code during constant evaluation because it confused return by reference with taking address of stack object

via llvm-bugs llvm-bugs at lists.llvm.org
Wed Feb 24 06:36:18 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=49342

            Bug ID: 49342
           Summary: Clang incorrectly rejects code during constant
                    evaluation because it confused return by reference
                    with taking address of stack object
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: mschellenbergercosta at googlemail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Created attachment 24568
  --> https://bugs.llvm.org/attachment.cgi?id=24568&action=edit
reproducer

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:
```cpp
#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+=`
```cpp
    _CONSTEXPR20_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
```cpp
#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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210224/e0329490/attachment.html>


More information about the llvm-bugs mailing list