Bill Fisher: This patch fixes a less likely case where '\b' can back up into invalid memory, when driven by a regex_iterator (for case 1, see r185273 or http://llvm.org/bugs/show_bug.cgi?id=16240)

The attached test program also supplies a test for the case 1 fix in r185273.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@186089 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Howard Hinnant 2013-07-11 15:32:55 +00:00
parent 37c17ed07d
commit e840208989
2 changed files with 66 additions and 2 deletions

View File

@ -5811,9 +5811,10 @@ regex_search(_BidirectionalIterator __first, _BidirectionalIterator __last,
const basic_regex<_CharT, _Traits>& __e,
regex_constants::match_flag_type __flags = regex_constants::match_default)
{
basic_string<_CharT> __s(__first, __last);
int __offset = (__flags & regex_constants::match_prev_avail) ? 1 : 0;
basic_string<_CharT> __s(_VSTD::prev(__first, __offset), __last);
match_results<const _CharT*> __mc;
bool __r = __e.__search(__s.data(), __s.data() + __s.size(), __mc, __flags);
bool __r = __e.__search(__s.data() + __offset, __s.data() + __s.size(), __mc, __flags);
__m.__assign(__first, __last, __mc, __flags & regex_constants::__no_update_pos);
return __r;
}

View File

@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// <regex>
// template <class BidirectionalIterator, class Allocator, class charT, class traits>
// bool
// regex_search(BidirectionalIterator first, BidirectionalIterator last,
// match_results<BidirectionalIterator, Allocator>& m,
// const basic_regex<charT, traits>& e,
// regex_constants::match_flag_type flags = regex_constants::match_default);
#include <regex>
#include <string>
#include <list>
#include <cassert>
int main()
{
// This regex_iterator uses regex_search(__wrap_iter<_Iter> __first, ...)
// Test for http://llvm.org/bugs/show_bug.cgi?id=16240 fixed in r185273.
{
std::string s{"aaaa a"};
std::regex re{"\\ba"};
std::sregex_iterator it{s.begin(), s.end(), re};
std::sregex_iterator end{};
assert(it->position(0) == 0);
assert(it->length(0) == 1);
++it;
assert(it->position(0) == 5);
assert(it->length(0) == 1);
++it;
assert(it == end);
}
// This regex_iterator uses regex_search(_BidirectionalIterator __first, ...)
{
std::string s{"aaaa a"};
std::list<char> l{s.begin(), s.end()};
std::regex re{"\\ba"};
std::regex_iterator<std::list<char>::iterator> it{l.begin(), l.end(), re};
std::regex_iterator<std::list<char>::iterator> end{};
assert(it->position(0) == 0);
assert(it->length(0) == 1);
++it;
assert(it->position(0) == 5);
assert(it->length(0) == 1);
++it;
assert(it == end);
}
}