[libcxx] r186089 - 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)
Howard Hinnant
hhinnant at apple.com
Thu Jul 11 08:32:55 PDT 2013
Author: hhinnant
Date: Thu Jul 11 10:32:55 2013
New Revision: 186089
URL: http://llvm.org/viewvc/llvm-project?rev=186089&view=rev
Log:
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.
Added:
libcxx/trunk/test/re/re.alg/re.alg.search/backup.pass.cpp
Modified:
libcxx/trunk/include/regex
Modified: libcxx/trunk/include/regex
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/regex?rev=186089&r1=186088&r2=186089&view=diff
==============================================================================
--- libcxx/trunk/include/regex (original)
+++ libcxx/trunk/include/regex Thu Jul 11 10:32:55 2013
@@ -5811,9 +5811,10 @@ regex_search(_BidirectionalIterator __fi
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;
}
Added: libcxx/trunk/test/re/re.alg/re.alg.search/backup.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/re/re.alg/re.alg.search/backup.pass.cpp?rev=186089&view=auto
==============================================================================
--- libcxx/trunk/test/re/re.alg/re.alg.search/backup.pass.cpp (added)
+++ libcxx/trunk/test/re/re.alg/re.alg.search/backup.pass.cpp Thu Jul 11 10:32:55 2013
@@ -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);
+ }
+}
More information about the cfe-commits
mailing list