From e840208989a8246e8b8dc45e2d9188afb334e02d Mon Sep 17 00:00:00 2001 From: Howard Hinnant Date: Thu, 11 Jul 2013 15:32:55 +0000 Subject: [PATCH] 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 --- include/regex | 5 +- test/re/re.alg/re.alg.search/backup.pass.cpp | 63 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 test/re/re.alg/re.alg.search/backup.pass.cpp diff --git a/include/regex b/include/regex index 36d56770..19e08b13 100644 --- a/include/regex +++ b/include/regex @@ -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 __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; } diff --git a/test/re/re.alg/re.alg.search/backup.pass.cpp b/test/re/re.alg/re.alg.search/backup.pass.cpp new file mode 100644 index 00000000..6bfad422 --- /dev/null +++ b/test/re/re.alg/re.alg.search/backup.pass.cpp @@ -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. +// +//===----------------------------------------------------------------------===// + +// + +// template +// bool +// regex_search(BidirectionalIterator first, BidirectionalIterator last, +// match_results& m, +// const basic_regex& e, +// regex_constants::match_flag_type flags = regex_constants::match_default); + +#include +#include +#include +#include + +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 l{s.begin(), s.end()}; + std::regex re{"\\ba"}; + std::regex_iterator::iterator> it{l.begin(), l.end(), re}; + std::regex_iterator::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); + } +}