[patch] Fix exception-safety issue in std::forward_list

Jonathan Wakely jwakely@redhat.com
Wed Jun 17 17:45:00 GMT 2015


There's a potential memory leak in the allocator-extended move
constructor of forward_list, if the allocator parameter is not equal
to the rvalue list's allocator then new list nodes must be allocated,
but if doing so throws then the already-allocated nodes are not freed.

Rather than adding a try-catch block this moves the allocations out of
the _Fwd_list_base constructor and into the derived constructor, so
that if an exception is thrown the base class will clean everything
up.

This also splits out the default constructor from the one taking an
allocator, so the default constructor is not explicit (as per the
resolution of LWG 2193) and can be conditionally-noexcept (as per LWG
2455) but the one taking an allocator is explicit and noexcept
(because copying allocators can't throw).

There are also some small optimisations:

The base constructors that take an allocator can take it by rvalue
reference and move from it, because we always have to construct a
temporary _Node_alloc_type from the user-supplied allocator.

The call to std::swap in _M_move_assign can be replaced with two
assignments, because we know one of the pointers is null so there's no
need to use a temporary variable to preserve its value.

Tested powerpc64le-linux, committed to trunk.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: patch.txt
Type: text/x-patch
Size: 6220 bytes
Desc: not available
URL: <http://gcc.gnu.org/pipermail/libstdc++/attachments/20150617/7dd2b987/attachment.bin>


More information about the Libstdc++ mailing list