summaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/c++17
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-04-07 16:05:42 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-04-07 16:39:24 +0100
commite06d3f5dd7d0c6b4a20fe813e6ee5addd097f560 (patch)
treeaa9c5749be11e6bf194005c90983845beb5f8cfd /libstdc++-v3/src/c++17
parent2f3d9104610cb2058cf091707a20c1c6eff8d470 (diff)
libstdc++: Fix filesystem::path construction from COW string [PR 99805]
Calling the non-const data() member on a COW string makes it "leaked", possibly resulting in reallocating the string to ensure a unique owner. The path::_M_split_cmpts() member parses its _M_pathname string using string_view objects and then calls _M_pathname.data() to find the offset of each string_view from the start of the string. However because _M_pathname is non-const that will cause a COW string to reallocate if it happens to be shared with another string object. This results in the offsets calculated for each component being wrong (i.e. undefined) because the string views no longer refer to substrings of the _M_pathname member. The fix is to use the parse.offset(c) member which gets the offset safely. The bug only happens for the path(string_type&&) constructor and only for COW strings. When constructed from an lvalue string the string's contents are copied rather than just incrementing the refcount, so there's no reallocation when calling the non-const data() member. The testsuite changes check the lvalue case anyway, because we should probably change the deep copying to just be a refcount increment (by adding a path(const string_type&) constructor or an overload for __effective_range(const string_type&), for COW strings only). libstdc++-v3/ChangeLog: PR libstdc++/99805 * src/c++17/fs_path.cc (path::_M_split_cmpts): Do not call non-const member on _M_pathname, to avoid copy-on-write. * testsuite/27_io/filesystem/path/decompose/parent_path.cc: Check construction from strings that might be shared.
Diffstat (limited to 'libstdc++-v3/src/c++17')
-rw-r--r--libstdc++-v3/src/c++17/fs_path.cc10
1 files changed, 4 insertions, 6 deletions
diff --git a/libstdc++-v3/src/c++17/fs_path.cc b/libstdc++-v3/src/c++17/fs_path.cc
index 2d9e29d9e7a..506ff25f9a6 100644
--- a/libstdc++-v3/src/c++17/fs_path.cc
+++ b/libstdc++-v3/src/c++17/fs_path.cc
@@ -1907,10 +1907,9 @@ path::_M_split_cmpts()
_M_cmpts.type(_Type::_Multi);
_M_cmpts.reserve(_M_cmpts.size() + buf.size());
auto output = _M_cmpts._M_impl->end();
- for (auto& c : buf)
+ for (const auto& c : buf)
{
- auto pos = c.str.data() - _M_pathname.data();
- ::new(output++) _Cmpt(c.str, c.type, pos);
+ ::new(output++) _Cmpt(c.str, c.type, parser.offset(c));
++_M_cmpts._M_impl->_M_size;
}
next = buf.begin();
@@ -1930,9 +1929,8 @@ path::_M_split_cmpts()
auto output = _M_cmpts._M_impl->end();
for (int i = 0; i < n; ++i)
{
- auto c = buf[i];
- auto pos = c.str.data() - _M_pathname.data();
- ::new(output++) _Cmpt(c.str, c.type, pos);
+ const auto& c = buf[i];
+ ::new(output++) _Cmpt(c.str, c.type, parser.offset(c));
++_M_cmpts._M_impl->_M_size;
}
}