Skip to content

Commit

Permalink
Report kRegexpBadPerlOp for look-behind assertions.
Browse files Browse the repository at this point in the history
Fixes #468.

Change-Id: I9a72db0bbb9a04e5081cc1f0f94399476b63d1c3
Reviewed-on: https://code-review.googlesource.com/c/re2/+/62330
Reviewed-by: Perry Lorier <[email protected]>
Reviewed-by: Paul Wankadia <[email protected]>
  • Loading branch information
junyer committed Jan 2, 2024
1 parent c217103 commit c042630
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions re2/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,17 @@ bool Regexp::ParseState::ParsePerlFlags(absl::string_view* s) {
return false;
}

// Check for look-around assertions. This is NOT because we support them! ;)
// As per https://github.com/google/re2/issues/468, we really want to report
// kRegexpBadPerlOp (not kRegexpBadNamedCapture) for look-behind assertions.
// Additionally, it would be nice to report not "(?<", but "(?<=" or "(?<!".
if ((t.size() > 3 && (t[2] == '=' || t[2] == '!')) ||
(t.size() > 4 && t[2] == '<' && (t[3] == '=' || t[3] == '!'))) {
status_->set_code(kRegexpBadPerlOp);
status_->set_error_arg(absl::string_view(t.data(), t[2] == '<' ? 4 : 3));
return false;
}

// Check for named captures, first introduced in Python's regexp library.
// As usual, there are three slightly different syntaxes:
//
Expand Down
26 changes: 26 additions & 0 deletions re2/testing/parse_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,30 @@ TEST(NamedCaptures, ErrorArgs) {
EXPECT_EQ(status.error_arg(), "(?<space bar>");
}

// Test that look-around error args are correct.
TEST(LookAround, ErrorArgs) {
RegexpStatus status;
Regexp* re;

re = Regexp::Parse("(?=foo).*", Regexp::LikePerl, &status);
EXPECT_TRUE(re == NULL);
EXPECT_EQ(status.code(), kRegexpBadPerlOp);
EXPECT_EQ(status.error_arg(), "(?=");

re = Regexp::Parse("(?!foo).*", Regexp::LikePerl, &status);
EXPECT_TRUE(re == NULL);
EXPECT_EQ(status.code(), kRegexpBadPerlOp);
EXPECT_EQ(status.error_arg(), "(?!");

re = Regexp::Parse("(?<=foo).*", Regexp::LikePerl, &status);
EXPECT_TRUE(re == NULL);
EXPECT_EQ(status.code(), kRegexpBadPerlOp);
EXPECT_EQ(status.error_arg(), "(?<=");

re = Regexp::Parse("(?<!foo).*", Regexp::LikePerl, &status);
EXPECT_TRUE(re == NULL);
EXPECT_EQ(status.code(), kRegexpBadPerlOp);
EXPECT_EQ(status.error_arg(), "(?<!");
}

} // namespace re2

0 comments on commit c042630

Please sign in to comment.