93 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: no-exceptions10// <regex>11 12// template <class charT, class traits = regex_traits<charT>> class basic_regex;13 14// template <class ST, class SA>15// basic_regex(const basic_string<charT, ST, SA>& s);16 17#include <regex>18#include <cassert>19#include "test_macros.h"20 21static bool error_badbackref_thrown(const char *pat, std::regex::flag_type f)22{23 bool result = false;24 try {25 std::regex re(pat, f);26 } catch (const std::regex_error &ex) {27 result = (ex.code() == std::regex_constants::error_backref);28 }29 return result;30}31 32int main(int, char**)33{34// no references35 assert(error_badbackref_thrown("\\1abc", std::regex_constants::ECMAScript));36 assert(error_badbackref_thrown("\\1abd", std::regex::basic));37 assert(error_badbackref_thrown("\\1abd", std::regex::extended));38 assert(error_badbackref_thrown("\\1abd", std::regex::awk) == false);39 assert(error_badbackref_thrown("\\1abd", std::regex::grep));40 assert(error_badbackref_thrown("\\1abd", std::regex::egrep));41 42// only one reference43 assert(error_badbackref_thrown("ab(c)\\2def", std::regex_constants::ECMAScript));44 assert(error_badbackref_thrown("ab\\(c\\)\\2def", std::regex_constants::basic));45 assert(error_badbackref_thrown("ab(c)\\2def", std::regex_constants::extended));46 assert(error_badbackref_thrown("ab\\(c\\)\\2def", std::regex_constants::awk) == false);47 assert(error_badbackref_thrown("ab(c)\\2def", std::regex_constants::awk) == false);48 assert(error_badbackref_thrown("ab\\(c\\)\\2def", std::regex_constants::grep));49 assert(error_badbackref_thrown("ab(c)\\2def", std::regex_constants::egrep));50 51 52 assert(error_badbackref_thrown("\\800000000000000000000000000000", std::regex_constants::ECMAScript)); // overflows53 54// this should NOT throw, because we only should look at the '1'55// See https://llvm.org/PR3138756 {57 const char *pat1 = "a(b)c\\1234";58 std::regex re(pat1, pat1 + 7); // extra chars after the end.59 }60 61// reference before group62 assert(error_badbackref_thrown("\\1(abc)", std::regex_constants::ECMAScript));63 assert(error_badbackref_thrown("\\1\\(abd\\)", std::regex::basic));64 assert(error_badbackref_thrown("\\1(abd)", std::regex::extended));65 assert(error_badbackref_thrown("\\1(abd)", std::regex::awk) == false);66 assert(error_badbackref_thrown("\\1\\(abd\\)", std::regex::awk) == false);67 assert(error_badbackref_thrown("\\1\\(abd\\)", std::regex::grep));68 assert(error_badbackref_thrown("\\1(abd)", std::regex::egrep));69 70// reference limit71 assert(error_badbackref_thrown("(cat)\\10", std::regex::ECMAScript));72 assert(error_badbackref_thrown("\\(cat\\)\\10", std::regex::basic) == false);73 assert(error_badbackref_thrown("(cat)\\10", std::regex::extended) == false);74 assert(error_badbackref_thrown("\\(cat\\)\\10", std::regex::awk) == false);75 assert(error_badbackref_thrown("(cat)\\10", std::regex::awk) == false);76 assert(error_badbackref_thrown("\\(cat\\)\\10", std::regex::grep) == false);77 assert(error_badbackref_thrown("(cat)\\10", std::regex::egrep) == false);78 79// https://llvm.org/PR3429780 assert(error_badbackref_thrown("(cat)\\1", std::regex::basic));81 assert(error_badbackref_thrown("\\(cat\\)\\1", std::regex::basic) == false);82 assert(error_badbackref_thrown("(cat)\\1", std::regex::extended) == false);83 assert(error_badbackref_thrown("\\(cat\\)\\1", std::regex::extended));84 assert(error_badbackref_thrown("(cat)\\1", std::regex::awk) == false);85 assert(error_badbackref_thrown("\\(cat\\)\\1", std::regex::awk) == false);86 assert(error_badbackref_thrown("(cat)\\1", std::regex::grep));87 assert(error_badbackref_thrown("\\(cat\\)\\1", std::regex::grep) == false);88 assert(error_badbackref_thrown("(cat)\\1", std::regex::egrep) == false);89 assert(error_badbackref_thrown("\\(cat\\)\\1", std::regex::egrep));90 91 return 0;92}93