Skip to content

Commit

Permalink
Lock out \K in lookaround assertions by default, but provide an optio…
Browse files Browse the repository at this point in the history
…n to

re-enable the old behaviour, just in case.
  • Loading branch information
PhilipHazel committed Aug 30, 2021
1 parent eea410b commit 21c2669
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 73 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ mode in the interpreters. Instead of just remembering whether one case matched
or not, it remembers the position of a previous match so as to avoid
unnecessary repeated searching.

6. Perl now locks out \K in lookarounds, so PCRE2 now does the same by default.
However, just in case anybody was relying on the old behaviour, there is an
option called PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK that enables the old behaviour.


Version 10.37 26-May-2021
-------------------------
Expand Down
11 changes: 9 additions & 2 deletions doc/pcre2api.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH PCRE2API 3 "28 August 2021" "PCRE2 10.38"
.TH PCRE2API 3 "30 August 2021" "PCRE2 10.38"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.sp
Expand Down Expand Up @@ -1875,6 +1875,13 @@ characters with code points greater than 127.
.sp
The option bits that can be set in a compile context by calling the
\fBpcre2_set_compile_extra_options()\fP function are as follows:
.sp
PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
.sp
Since release 10.38 PCRE2 has forbidden the use of \eK within lookaround
assertions, following Perl's lead. This option is provided to re-enable the
previous behaviour (act in positive lookarounds, ignore in negative ones) in
case anybody is relying on it.
.sp
PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES
.sp
Expand Down Expand Up @@ -4009,6 +4016,6 @@ Cambridge, England.
.rs
.sp
.nf
Last updated: 28 August 2021
Last updated: 30 August 2021
Copyright (c) 1997-2021 University of Cambridge.
.fi
14 changes: 8 additions & 6 deletions doc/pcre2compat.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH PCRE2COMPAT 3 "06 October 2020" "PCRE2 10.36"
.TH PCRE2COMPAT 3 "30 August 2021" "PCRE2 10.38"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH "DIFFERENCES BETWEEN PCRE2 AND PERL"
Expand Down Expand Up @@ -133,8 +133,10 @@ in the release at the time of writing (5.32), \ep{Lu} and \ep{Ll} match all
letters, regardless of case, when case independence is specified.
.P
16. From release 5.32.0, Perl locks out the use of \eK in lookaround
assertions. In PCRE2, \eK is acted on when it occurs in positive assertions,
but is ignored in negative assertions.
assertions. From release 10.38 PCRE2 does the same by default. However, there
is an option for re-enabling the previous behaviour. When this option is set,
\eK is acted on when it occurs in positive assertions, but is ignored in
negative assertions.
.P
17. PCRE2 provides some extensions to the Perl regular expression facilities.
Perl 5.10 included new features that were not in earlier versions of Perl, some
Expand Down Expand Up @@ -203,7 +205,7 @@ fall into any stack-overflow limit. PCRE2 made a similar change at release
.sp
.nf
Philip Hazel
University Computing Service
Retired from University Computing Service
Cambridge, England.
.fi
.
Expand All @@ -212,6 +214,6 @@ Cambridge, England.
.rs
.sp
.nf
Last updated: 06 October 2020
Copyright (c) 1997-2019 University of Cambridge.
Last updated: 30 August 2021
Copyright (c) 1997-2021 University of Cambridge.
.fi
16 changes: 9 additions & 7 deletions doc/pcre2pattern.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH PCRE2PATTERN 3 "06 October 2020" "PCRE2 10.35"
.TH PCRE2PATTERN 3 "3o0 August 2021" "PCRE2 10.38"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH "PCRE2 REGULAR EXPRESSION DETAILS"
Expand Down Expand Up @@ -1168,9 +1168,11 @@ For example, when the pattern
.sp
matches "foobar", the first substring is still set to "foo".
.P
Perl used to document that the use of \eK within lookaround assertions is "not
well defined", but from version 5.32.0 Perl does not support this usage at all.
In PCRE2, \eK is acted upon when it occurs inside positive assertions, but is
From version 5.32.0 Perl forbids the use of \eK in lookaround assertions. From
release 10.38 PCRE2 also forbids this by default. However, the
PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK option can be used when calling
\fBpcre2_compile()\fP to re-enable the previous behaviour. When this option is
set, \eK is acted upon when it occurs inside positive assertions, but is
ignored in negative assertions. Note that when a pattern such as (?=ab\eK)
matches, the reported start of the match can be greater than the end of the
match. Using \eK in a lookbehind assertion at the start of a pattern can also
Expand Down Expand Up @@ -3889,7 +3891,7 @@ there is a backtrack at the outer level.
.sp
.nf
Philip Hazel
University Computing Service
Retired from University Computing Service
Cambridge, England.
.fi
.
Expand All @@ -3898,6 +3900,6 @@ Cambridge, England.
.rs
.sp
.nf
Last updated: 06 October 2020
Copyright (c) 1997-2020 University of Cambridge.
Last updated: 30 August 2021
Copyright (c) 1997-2021 University of Cambridge.
.fi
11 changes: 7 additions & 4 deletions doc/pcre2syntax.3
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH PCRE2SYNTAX 3 "28 December 2019" "PCRE2 10.35"
.TH PCRE2SYNTAX 3 "30 August 2021" "PCRE2 10.38"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH "PCRE2 REGULAR EXPRESSION SYNTAX SUMMARY"
Expand Down Expand Up @@ -401,6 +401,9 @@ but some of them use Unicode properties if PCRE2_UCP is set. You can use
.sp
\eK set reported start of match
.sp
From release 10.38 \eK is not permitted by default in lookaround assertions,
for compatibility with Perl. However, if the PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
option is set, the previous behaviour is re-enabled. When this option is set,
\eK is honoured in positive assertions, but ignored in negative ones.
.
.
Expand Down Expand Up @@ -667,7 +670,7 @@ delimiter }. To encode the ending delimiter within the string, double it.
.sp
.nf
Philip Hazel
University Computing Service
Retired from University Computing Service
Cambridge, England.
.fi
.
Expand All @@ -676,6 +679,6 @@ Cambridge, England.
.rs
.sp
.nf
Last updated: 28 December 2019
Copyright (c) 1997-2019 University of Cambridge.
Last updated: 30 August 2021
Copyright (c) 1997-2021 University of Cambridge.
.fi
30 changes: 13 additions & 17 deletions doc/pcre2test.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH PCRE2TEST 1 "28 August 2021" "PCRE 10.38"
.TH PCRE2TEST 1 "30 August 2021" "PCRE 10.38"
.SH NAME
pcre2test - a program for testing Perl-compatible regular expressions.
.SH SYNOPSIS
Expand Down Expand Up @@ -27,12 +27,7 @@ each match attempt. Modifiers on external or internal command lines, the
patterns, and the subject lines specify PCRE2 function options, control how the
subject is processed, and what output is produced.
.P
As the original fairly simple PCRE library evolved, it acquired many different
features, and as a result, the original \fBpcretest\fP program ended up with a
lot of options in a messy, arcane syntax for testing all the features. The
move to the new PCRE2 API provided an opportunity to re-implement the test
program as \fBpcre2test\fP, with a cleaner modifier syntax. Nevertheless, there
are still many obscure modifiers, some of which are specifically designed for
There are many obscure modifiers, some of which are specifically designed for
use in conjunction with the test script and data files that are distributed as
part of PCRE2. All the modifiers are documented here, some without much
justification, but many of them are unlikely to be of use except when testing
Expand Down Expand Up @@ -61,10 +56,10 @@ names used in the libraries have a suffix _8, _16, or _32, as appropriate.
.rs
.sp
Input to \fBpcre2test\fP is processed line by line, either by calling the C
library's \fBfgets()\fP function, or via the \fBlibreadline\fP library. In some
Windows environments character 26 (hex 1A) causes an immediate end of file, and
no further data is read, so this character should be avoided unless you really
want that action.
library's \fBfgets()\fP function, or via the \fBlibreadline\fP or \fBlibedit\fP
library. In some Windows environments character 26 (hex 1A) causes an immediate
end of file, and no further data is read, so this character should be avoided
unless you really want that action.
.P
The input is processed using using C's string functions, so must not
contain binary zeros, even though in Unix-like environments, \fBfgets()\fP
Expand Down Expand Up @@ -472,11 +467,11 @@ A pattern can be followed by a modifier list (details below).
.SH "SUBJECT LINE SYNTAX"
.rs
.sp
Before each subject line is passed to \fBpcre2_match()\fP or
\fBpcre2_dfa_match()\fP, leading and trailing white space is removed, and the
line is scanned for backslash escapes, unless the \fBsubject_literal\fP
modifier was set for the pattern. The following provide a means of encoding
non-printing characters in a visible way:
Before each subject line is passed to \fBpcre2_match()\fP,
\fBpcre2_dfa_match()\fP, or \fBpcre2_jit_match()\fP, leading and trailing white
space is removed, and the line is scanned for backslash escapes, unless the
\fBsubject_literal\fP modifier was set for the pattern. The following provide a
means of encoding non-printing characters in a visible way:
.sp
\ea alarm (BEL, \ex07)
\eb backspace (\ex08)
Expand Down Expand Up @@ -572,6 +567,7 @@ way \fBpcre2_compile()\fP behaves. See
for a description of the effects of these options.
.sp
allow_empty_class set PCRE2_ALLOW_EMPTY_CLASS
allow_lookaround_bsk set PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
allow_surrogate_escapes set PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES
alt_bsux set PCRE2_ALT_BSUX
alt_circumflex set PCRE2_ALT_CIRCUMFLEX
Expand Down Expand Up @@ -2107,6 +2103,6 @@ Cambridge, England.
.rs
.sp
.nf
Last updated: 28 August 2021
Last updated: 30 August 2021
Copyright (c) 1997-2021 University of Cambridge.
.fi
4 changes: 3 additions & 1 deletion src/pcre2.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/* This is the public header file for the PCRE library, second API, to be
#included by applications that call PCRE2 functions.

Copyright (c) 2016-2020 University of Cambridge
Copyright (c) 2016-2021 University of Cambridge

-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -152,6 +152,7 @@ D is inspected during pcre2_dfa_match() execution
#define PCRE2_EXTRA_MATCH_LINE 0x00000008u /* C */
#define PCRE2_EXTRA_ESCAPED_CR_IS_LF 0x00000010u /* C */
#define PCRE2_EXTRA_ALT_BSUX 0x00000020u /* C */
#define PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK 0x00000040u /* C */

/* These are for pcre2_jit_compile(). */

Expand Down Expand Up @@ -311,6 +312,7 @@ pcre2_pattern_convert(). */
#define PCRE2_ERROR_SCRIPT_RUN_NOT_AVAILABLE 196
#define PCRE2_ERROR_TOO_MANY_CAPTURES 197
#define PCRE2_ERROR_CONDITION_ATOMIC_ASSERTION_EXPECTED 198
#define PCRE2_ERROR_BACKSLASH_K_IN_LOOKAROUND 199


/* "Expected" matching error codes: no match and partial match. */
Expand Down
21 changes: 17 additions & 4 deletions src/pcre2_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and semantics are as close as possible to those of the Perl 5 language.
Written by Philip Hazel
Original API code Copyright (c) 1997-2012 University of Cambridge
New API code Copyright (c) 2016-2020 University of Cambridge
New API code Copyright (c) 2016-2021 University of Cambridge
-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -782,12 +782,15 @@ are allowed. */
#define PUBLIC_COMPILE_EXTRA_OPTIONS \
(PUBLIC_LITERAL_COMPILE_EXTRA_OPTIONS| \
PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES|PCRE2_EXTRA_BAD_ESCAPE_IS_LITERAL| \
PCRE2_EXTRA_ESCAPED_CR_IS_LF|PCRE2_EXTRA_ALT_BSUX)
PCRE2_EXTRA_ESCAPED_CR_IS_LF|PCRE2_EXTRA_ALT_BSUX| \
PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK)

/* Compile time error code numbers. They are given names so that they can more
easily be tracked. When a new number is added, the tables called eint1 and
eint2 in pcre2posix.c may need to be updated, and a new error text must be
added to compile_error_texts in pcre2_error.c. */
added to compile_error_texts in pcre2_error.c. Also, the error codes in
pcre2.h.in must be updated - their values are exactly 100 greater than these
values. */

enum { ERR0 = COMPILE_ERROR_BASE,
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10,
Expand All @@ -799,7 +802,7 @@ enum { ERR0 = COMPILE_ERROR_BASE,
ERR61, ERR62, ERR63, ERR64, ERR65, ERR66, ERR67, ERR68, ERR69, ERR70,
ERR71, ERR72, ERR73, ERR74, ERR75, ERR76, ERR77, ERR78, ERR79, ERR80,
ERR81, ERR82, ERR83, ERR84, ERR85, ERR86, ERR87, ERR88, ERR89, ERR90,
ERR91, ERR92, ERR93, ERR94, ERR95, ERR96, ERR97, ERR98 };
ERR91, ERR92, ERR93, ERR94, ERR95, ERR96, ERR97, ERR98, ERR99 };

/* This is a table of start-of-pattern options such as (*UTF) and settings such
as (*LIMIT_MATCH=nnnn) and (*CRLF). For completeness and backward
Expand Down Expand Up @@ -7799,6 +7802,16 @@ for (;; pptr++)
}
#endif

/* \K is forbidden in lookarounds since 10.38 because that's what Perl has
done. However, there's an option, in case anyone was relying on it. */

if (cb->assert_depth > 0 && meta_arg == ESC_K &&
(cb->cx->extra_options & PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK) == 0)
{
*errorcodeptr = ERR99;
return 0;
}

/* For the rest (including \X when Unicode is supported - if not it's
faulted at parse time), the OP value is the escape value when PCRE2_UCP is
not set; if it is set, these escapes do not show up here because they are
Expand Down
3 changes: 2 additions & 1 deletion src/pcre2_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and semantics are as close as possible to those of the Perl 5 language.
Written by Philip Hazel
Original API code Copyright (c) 1997-2012 University of Cambridge
New API code Copyright (c) 2016-2019 University of Cambridge
New API code Copyright (c) 2016-2021 University of Cambridge
-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -186,6 +186,7 @@ static const unsigned char compile_error_texts[] =
"script runs require Unicode support, which this version of PCRE2 does not have\0"
"too many capturing groups (maximum 65535)\0"
"atomic assertion expected after (?( or (?(?C)\0"
"\\K is not allowed in lookarounds (but see PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK)\0"
;

/* Match-time and UTF error texts are in the same format. */
Expand Down
9 changes: 6 additions & 3 deletions src/pcre2demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,12 @@ pcre2_match_data_create_from_pattern() above. */
if (rc == 0)
printf("ovector was not big enough for all the captured substrings\n");

/* We must guard against patterns such as /(?=.\K)/ that use \K in an assertion
to set the start of a match later than its end. In this demonstration program,
we just detect this case and give up. */
/* Since release 10.38 PCRE2 has locked out the use of \K in lookaround
assertions. However, there is an option to re-enable the old behaviour. If that
is set, it is possible to run patterns such as /(?=.\K)/ that use \K in an
assertion to set the start of a match later than its end. In this demonstration
program, we show how to detect this case, but it shouldn't arise because the
option is never set. */

if (ovector[0] > ovector[1])
{
Expand Down
1 change: 1 addition & 0 deletions src/pcre2posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ static const int eint2[] = {
37, REG_EESCAPE, /* PCRE2 does not support \L, \l, \N{name}, \U, or \u */
56, REG_INVARG, /* internal error: unknown newline setting */
92, REG_INVARG, /* invalid option bits with PCRE2_LITERAL */
99, REG_EESCAPE /* \K in lookaround */
};

/* Table of texts corresponding to POSIX error codes */
Expand Down
3 changes: 2 additions & 1 deletion src/pcre2test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ hacked-up (non-) design had also run out of steam.
Written by Philip Hazel
Original code Copyright (c) 1997-2012 University of Cambridge
Rewritten code Copyright (c) 2016-2020 University of Cambridge
Rewritten code Copyright (c) 2016-2021 University of Cambridge
-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -622,6 +622,7 @@ static modstruct modlist[] = {
{ "allaftertext", MOD_PNDP, MOD_CTL, CTL_ALLAFTERTEXT, PO(control) },
{ "allcaptures", MOD_PND, MOD_CTL, CTL_ALLCAPTURES, PO(control) },
{ "allow_empty_class", MOD_PAT, MOD_OPT, PCRE2_ALLOW_EMPTY_CLASS, PO(options) },
{ "allow_lookaround_bsk", MOD_CTC, MOD_OPT, PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK, CO(extra_options) },
{ "allow_surrogate_escapes", MOD_CTC, MOD_OPT, PCRE2_EXTRA_ALLOW_SURROGATE_ESCAPES, CO(extra_options) },
{ "allusedtext", MOD_PNDP, MOD_CTL, CTL_ALLUSEDTEXT, PO(control) },
{ "allvector", MOD_PND, MOD_CTL, CTL2_ALLVECTOR, PO(control2) },
Expand Down
3 changes: 0 additions & 3 deletions testdata/testinput18
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@
//posix_nosub
\=offset=70000

/(?=(a\K))/
a

/^d(e)$/posix
acdef\=posix_startend=2:4
acde\=posix_startend=2
Expand Down
Loading

0 comments on commit 21c2669

Please sign in to comment.