[Bug libstdc++/120029] Dangling iterator usage in std::filesystem::path::operator+=(const std::filesystem::path &p) when this == p
redi at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Mar 16 23:18:29 GMT 2026
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120029
Jonathan Wakely <redi at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to work| |15.2.0
--- Comment #10 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I've just realised that we should be able to avoid the loop in the fix:
// Handle p += *i where i is in [p.begin(),p.end()), for the same reason.
if (_M_type() == _Type::_Multi && p._M_type() != _Type::_Multi)
for (const path& cmpt : *this)
if (&cmpt == &p) [[unlikely]]
return *this += p.native();
We store each component in a contiguous array, so we can just do:
if (_M_type() == _Type::_Multi && p._M_type() != _Type::_Multi)
{
const auto first = _M_cmpts.begin(), first + _M_cmpts.size();
if (!std::less<>()(&p, first) && std::less<>()(&p, last)) [[unlikely]]
return *this += p.native();
}
More information about the Gcc-bugs
mailing list