<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - regex does not throw an exception for an ECMAScript pattern ending in a trailing backslash"
   href="https://llvm.org/bugs/show_bug.cgi?id=26175">26175</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>regex does not throw an exception for an ECMAScript pattern ending in a trailing backslash
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>josh.petrie@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=15643" name="attach_15643" title="Changes I made to <regex> to produce the behavior I "think" is correct.">attachment 15643</a> <a href="attachment.cgi?id=15643&action=edit" title="Changes I made to <regex> to produce the behavior I "think" is correct.">[details]</a></span>
Changes I made to <regex> to produce the behavior I "think" is correct.

When constructing a std::regex object with the pattern "\\" or R"(\)" (that is,
a single backslash), no exception is thrown. I would expect a std::regex_error
to be thrown with the "error_escape" error code, because as far as I have been
able to determine a trailing backslash is not a valid pattern.

I first noticed this in Xcode 7.2 (7C68), compiling with C++14 enabled and the
system libc++ (on OS X 10.11.2). However, I was able to reproduce the issue
using a libc++ built from source (SVN revision 257997). 

The following program demonstrates the issue:

#include <iostream>
#include <regex>

int main () {
  try {
    std::regex example("\\");
  } catch (const std::regex_error & error) {
    // Never executes, but probably should:
    std::cout << "code: " << error.code() << "\n";
  }
}

It appears that __parse_atom_escape is the problematic function; it *seems*
that __parse_atom_escape should look something like:

template <class _CharT, class _Traits>
template <class _ForwardIterator>
_ForwardIterator
basic_regex<_CharT, _Traits>::__parse_atom_escape(_ForwardIterator __first,
                                                  _ForwardIterator __last)
{
    if (__first != __last && *__first == '\\')
    {
        _ForwardIterator __t1 = _VSTD::next(__first);
        if (__t1 == __last)                                   //!
            throw regex_error(regex_constants::error_escape); //!

        _ForwardIterator __t2 = __parse_decimal_escape(__t1, __last);
        ...

Where the additional two lines, marked with //!, check if advancing __first has
run in to the end of the expression, and if so throws the appropriate error.

(However my experience in hacking on the standard library is limited to
precisely the above, so I might be very, very off-base with this suggestion.)</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>