<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60526>60526</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            AddressSanitizer reports invalid-pointer-pair in std::string's __addr_in_range
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mplucinski
      </td>
    </tr>
</table>

<pre>
    (originally I asked on StackOverflow https://stackoverflow.com/q/75344685/1088139 where I got a suggestion to report it as a bug) 

With the latest master of clang and libc++, the following program makes Asan to report a problem:

```c++
#include <filesystem>
#include <iostream>

constexpr std::string_view str = "/usr";

const std::filesystem::path path{str};

int main() {
        std::cout << "Path: " << path << std::endl;
}
```

What happens:

```c++
$ clang++ -fsanitize=address,pointer-compare,pointer-subtract -g -O0 -std=c++20 repro-asan.cpp -stdlib=libc++ -o repro-asan
$ export ASAN_OPTIONS="verbosity=1:detect_invalid_pointer_pairs=2"
$ ./repro-asan
=================================================================
==3183863==ERROR: AddressSanitizer: invalid-pointer-pair: 0x563a8315eb41 0x563a82764600
error: failed to decompress '.debug_aranges', zlib is not available
error: failed to decompress '.debug_info', zlib is not available
error: failed to decompress '.debug_abbrev', zlib is not available
error: failed to decompress '.debug_line', zlib is not available
error: failed to decompress '.debug_str', zlib is not available
error: failed to decompress '.debug_line_str', zlib is not available
error: failed to decompress '.debug_loclists', zlib is not available
error: failed to decompress '.debug_rnglists', zlib is not available
    #0 0x563a8274d818 in bool std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>::__addr_in_range[abi:v170000]<char const&>(char const&) const /home/me/dev/llvm/build/bin/../include/c++/v1/string:1979:23
    #1 0x563a8274d418 in std::__1::enable_if<__is_cpp17_forward_iterator<char const*>::value, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&>::type std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>::append[abi:v170000]<char const*>(char const*, char const*) /home/me/dev/llvm/build/bin/../include/c++/v1/string:2805:14
 #2 0x563a8274d264 in std::__1::enable_if<__is_cpp17_forward_iterator<char const*>::value, void>::type std::__1::__fs::filesystem::_PathCVT<char>::__append_range[abi:v170000]<char const*>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&, char const*, char const*) /home/me/dev/llvm/build/bin/../include/c++/v1/__filesystem/path.h:316:12
 #3 0x563a8274cfcd in void std::__1::__fs::filesystem::_PathCVT<char>::__append_source[abi:v170000]<std::__1::basic_string_view<char, std::__1::char_traits<char>>>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&, std::__1::basic_string_view<char, std::__1::char_traits<char>> const&) /home/me/dev/llvm/build/bin/../include/c++/v1/__filesystem/path.h:331:5
 #4 0x563a8274cb2e in std::__1::__fs::filesystem::path::path[abi:v170000]<std::__1::basic_string_view<char, std::__1::char_traits<char>>, void>(std::__1::basic_string_view<char, std::__1::char_traits<char>> const&, std::__1::__fs::filesystem::path::format) /home/me/dev/llvm/build/bin/../include/c++/v1/__filesystem/path.h:483:5
 #5 0x563a82676438 in __cxx_global_var_init /home/me/dev/tmp/repro-asan.cpp:6:29
    #6 0x563a82676474 in _GLOBAL__sub_I_repro_asan.cpp /home/me/dev/tmp/repro-asan.cpp
    #7 0x7fca55896eba in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x29eba) (BuildId: 69389d485a9793dbe873f0ea2c93e02efaa9aa3d)
    #8 0x563a826764a4 in _start (/home/me/dev/tmp/repro-asan+0x1f4a4)

0x563a8315eb41 is located 1 bytes inside of global variable 'path' defined in '/home/me/dev/tmp/repro-asan.cpp:6' (0x563a8315eb40) of size 24
0x563a82764600 is located 32 bytes before global variable '.str.2' defined in '/home/me/dev/llvm/build/bin/../include/c++/v1/string:1984' (0x563a82764620) of size 13
  '.str.2' is ascii string 'basic_string'
0x563a82764600 is located 0 bytes inside of global variable '.str.1' defined in '/home/me/dev/tmp/repro-asan.cpp:4' (0x563a82764600) of size 5
  '.str.1' is ascii string '/usr'
SUMMARY: AddressSanitizer: invalid-pointer-pair /home/me/dev/llvm/build/bin/../include/c++/v1/string:1979:23 in bool std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>::__addr_in_range[abi:v170000]<char const&>(char const&) const
==3183863==ABORTING
```

System is Ubuntu 22.04 LTS
clang and libc++ built from commit c8aedf98ec4cfefe6a8d6e98dd4bf3d97e81c125 (2023-02-03, basically yesterday).
I also checked with a recent stable version of clang (15.0.6), but the problem doesn't occur there.

My understanding is that the comparison that happens in __addr_in_range isn't legal, since it touches unrelated pointers: https://github.com/llvm/llvm-project/blob/7f12efa88e17548d98f3e7425687f4afe0df34ed/libcxx/include/string#L1979

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWd1u2zgWfhrm5sCGROr3IhdOPB4EaJtB09nFXgkUeWRzK5NeknKSPv2ClOLYSTpNd9Lu7AKCLJPU-fs-Hh5S3Dm11ojnJL8g-fKMD35j7Pl21w9CafdZnbVG3p8TWhmr1krzvr-HK-DuM0owGm48F5-v92i73tzCxvudI2xB6IrQlQt9ZuqbC7MldPUvQldlzrKsqHJCV2lSVSmr4XaDFuEK1sYDBzes1-i8Mhq8AYs7Yz0oD9wBh3ZYE1oDSZYkWYz3vyu_Ab9B6LlH52HLnUcLpgPRc70GriX0qhWEXsTrMg7uTN-bW6XXsLNmbfkWtvwzOlg4fqyXh-62x23w60gpKZLxehA7tlKmtOgHiUDYZad6dPfOh5d_eWmAMs5b5Efd8S6Mdh7vdhacl0EvWzhvlV43e4W34LwFwpZAKCV0NTgbHtjFMwmPbx8bEv7vuN9AuJHywnlLyuWT95UOYVSa0CpGuzz01gehwgw-OEHYZTDltyCOLcLjQ2tUMz0fXkMt-0dt5fJJOE-A3XAPG77boXavDH82Yj42wqxzXCuvviBhSy6lRecIvdwZpT3amTDbHb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