Skip to content

Commit 5485538

Browse files
committed
Use std::less to detect string overlap in path::append.
Pointer ordering is unspecified if the pointers are to unrelated objects, which triggers ASAN warnings. Use std::less to avoid this. Closes #335.
1 parent 4816a1c commit 5485538

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

build/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ local cxx_requirements = [ requires
137137
cxx11_defaulted_moves
138138
cxx11_deleted_functions
139139
cxx11_function_template_default_args
140+
cxx11_unified_initialization_syntax
140141
cxx11_final
141142
cxx11_override
142143
] ;

doc/release_history.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
</tr>
4141
</table>
4242

43+
<h2>1.89.0</h2>
44+
<ul>
45+
<li>Corrected ASAN warnings about comparing pointers to potentially unrelated objects in <code>path::append</code>. (<a href="https://github.com/boostorg/filesystem/issues/335">#335</a>)</li>
46+
</ul>
47+
4348
<h2>1.87.0</h2>
4449
<ul>
4550
<li>As was announced in 1.84.0, Windows versions prior to 10 are no longer supported.</li>

src/path.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <boost/filesystem/detail/path_traits.hpp> // codecvt_error_category()
1616
#include <boost/system/error_category.hpp> // for BOOST_SYSTEM_HAS_CONSTEXPR
1717
#include <boost/assert.hpp>
18+
#include <functional>
1819
#include <algorithm>
1920
#include <iterator>
2021
#include <utility>
@@ -139,6 +140,13 @@ inline void first_element(string_type const& src, size_type& element_pos, size_t
139140
first_element(src, element_pos, element_size, src.size());
140141
}
141142

143+
// Checks if the second string overlaps in memory with the first string
144+
inline bool is_overlapping(string_type const& str, const value_type* arg)
145+
{
146+
std::less< const value_type* > ptr_less{};
147+
return !(ptr_less(arg, str.data()) || ptr_less(str.data() + str.size(), arg));
148+
}
149+
142150
} // unnamed namespace
143151

144152
//--------------------------------------------------------------------------------------//
@@ -478,7 +486,7 @@ BOOST_FILESYSTEM_DECL void path_algorithms::append_v3(path& p, const value_type*
478486
{
479487
if (begin != end)
480488
{
481-
if (BOOST_LIKELY(begin < p.m_pathname.data() || begin >= (p.m_pathname.data() + p.m_pathname.size())))
489+
if (BOOST_LIKELY(!is_overlapping(p.m_pathname, begin)))
482490
{
483491
if (!detail::is_directory_separator(*begin))
484492
path_algorithms::append_separator_if_needed(p);
@@ -497,7 +505,7 @@ BOOST_FILESYSTEM_DECL void path_algorithms::append_v4(path& p, const value_type*
497505
{
498506
if (begin != end)
499507
{
500-
if (BOOST_LIKELY(begin < p.m_pathname.data() || begin >= (p.m_pathname.data() + p.m_pathname.size())))
508+
if (BOOST_LIKELY(!is_overlapping(p.m_pathname, begin)))
501509
{
502510
const size_type that_size = end - begin;
503511
size_type that_root_name_size = 0;

0 commit comments

Comments
 (0)