title: 'Attempt to dereference a past-the-end iterator' when sorting a vector of filesystem paths
body: <p>I'm writing a simple file chooser using std::filesystem. The entries of the current directory are stored in a vector. When I try to sort the vector with std::sort the program crashes.</p>
  
  <p>This happens with g++-9 on Ubuntu 19.04. The file is compiled with the -D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC debug flags.</p>
  
  <p>The relevant code looks like this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;filesystem&gt;
  #include &lt;vector&gt;
  #include &lt;algorithm&gt;
  
  namespace fs = std::filesystem;
  
  struct FileBrowser {
      std::vector&lt;fs::path&gt; files;
  
      FileBrowser() {
          UpdateFiles();
      }
  
      void UpdateFiles() {
          files.clear();
          for (const auto&amp; entry : fs::directory_iterator(currentPath)) {
              files.push_back(entry.path());
          }
  
  
          std::sort(files.begin(), files.end(), [](const auto&amp; lhs, const auto&amp; rhs) {
              if (fs::is_directory(lhs) &amp;&amp; !fs::is_directory(rhs)) {
                  return 1;
              } else if (fs::is_directory(rhs) &amp;&amp; !fs::is_directory(lhs)) {
                  return 0;
              } else {
                  return lhs.filename().string().compare(rhs.filename().string());
              }
          });
      }
  };
  </code></pre>
  
  <p>The error message looks like this:</p>
  
  <pre><code>/usr/include/c++/9/debug/safe_iterator.h:294:
  In function:
      __gnu_debug::_Safe_iterator&lt;_Iterator, _Sequence, _Category&gt;::reference 
      __gnu_debug::_Safe_iterator&lt;_Iterator, _Sequence, 
      _Category&gt;::operator*() const [with _Iterator = 
      __gnu_cxx::__normal_iterator&lt;std::filesystem::__cxx11::path*, 
      std::__cxx1998::vector&lt;std::filesystem::__cxx11::path, 
      std::allocator&lt;std::filesystem::__cxx11::path&gt; &gt; &gt;; _Sequence = 
      std::__debug::vector&lt;std::filesystem::__cxx11::path&gt;; _Category = 
      std::forward_iterator_tag; __gnu_debug::_Safe_iterator&lt;_Iterator, 
      _Sequence, _Category&gt;::reference = std::filesystem::__cxx11::path&amp;]
  
  Error: attempt to dereference a past-the-end iterator.
  
  Objects involved in the operation:
      iterator "this" @ 0x0x7fff6c67d9d0 {
        type = __gnu_cxx::__normal_iterator&lt;std::filesystem::__cxx11::path*, std::__cxx1998::vector&lt;std::filesystem::__cxx11::path, std::allocator&lt;std::filesystem::__cxx11::path&gt; &gt; &gt; (mutable iterator);
        state = past-the-end;
        references sequence with type 'std::__debug::vector&lt;std::filesystem::__cxx11::path, std::allocator&lt;std::filesystem::__cxx11::path&gt; &gt;' @ 0x0x55ca5b4536c0
      }
  </code></pre>
  
  <p>I saw plenty of examples online where std::sort is called with vector.end(). I tried it with <code>files.end() - 1</code> and received the same error message.</p>