libc++ patch for LWG Issue 2232 - dangling reference for regex_iterators

Marshall Clow mclow.lists at gmail.com
Mon Feb 17 14:22:54 PST 2014


On Feb 14, 2014, at 4:31 PM, David Blaikie <dblaikie at gmail.com> wrote:
> On Fri, Feb 14, 2014 at 4:21 PM, Marshall Clow <mclow.lists at gmail.com> wrote:
> Users can write
>         for(sregex_iterator i(s.begin(), s.end(), regex("meow")), end; i != end; ++i)
> 
> binding a temporary regex to const regex& and storing a pointer to it.
> This will compile silently, triggering undefined behavior at runtime.
> 
> Fixing this involves defining constructors for the various regex iterator types from rvalue regexes, and then marking them as “deleted”.
> And tests.
> 
> 
> Would this break (what I assume is) correct code doing things like:
> 
> std::find(sregex_iterator(s.begin(), s.end(), regex("meow")), end, "foo”);

Yes, but while that code is technically “correct”, it’s not useful.

The return value from find is going to be an iterator that “points” into a destructed regex.
As soon as you use it for anything, you’re (hopefully) going to go boom.

The “fix” for this is easy.
	regex meow { “meow” };
	std::find(sregex_iterator(s.begin(), s.end(), meow), end, "foo”);

Now you can control the lifetime of the regex, and make sure that it doesn’t get destroyed too early.

For those following along at home, the standard issue (which was adopted for C++14) is here:
	http://cplusplus.github.io/LWG/lwg-active.html#2332

and a closely related one (also part of the C++14 standard):
	http://cplusplus.github.io/LWG/lwg-active.html#2329

Here’s a revised patch that covers both of them.

— Marshall


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140217/fddcb2d5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: LWG2332-2.patch
Type: application/octet-stream
Size: 19683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140217/fddcb2d5/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140217/fddcb2d5/attachment-0001.html>


More information about the cfe-commits mailing list