Link applications to PCRE2-specific symbols when using POSIX API

This commit is contained in:
Petr Písař 2019-01-22 15:49:54 +01:00
parent 90185e83f1
commit 51d82854ab
3 changed files with 420 additions and 1 deletions

View File

@ -0,0 +1,130 @@
From 3450125e8bfd7afd7eedadabac86575f74e28a06 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Tue, 22 Jan 2019 15:12:31 +0100
Subject: [PATCH] Declare POSIX regex function names as macros to PCRE
functions
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
POSIX regex libraries differ in regex_t size. If a program includes
<pcreposix.h>, but is not linked to pcreposix library at run-time
(either in effect of --as-needed or a lazy binding in dlopen)
other implementation touches memory out of the structure and the
program can crash.
That means once a program includes <pcreposix.h>, it must link to the
pcreposix library.
This patch replaces the POSIX regex declaration with macros to the
PCRE uniqely-named function. This ensures that the PCRE's regex_t
structure is always handled by the PCRE functions.
This patch still preserves the POSIX regex definitions in order to
preseve ABI with application compiled before this change. The
definition can be removed in the future.
Signed-off-by: Petr Písař <ppisar@redhat.com>
---
README | 12 ------------
src/pcre2posix.c | 7 +++++++
src/pcre2posix.h | 15 ++++++++-------
3 files changed, 15 insertions(+), 19 deletions(-)
diff --git a/README b/README
index 9c46ac4..b44392a 100644
--- a/README
+++ b/README
@@ -55,18 +55,6 @@ with existing files of that name by distributing it that way. To use PCRE2 with
an existing program that uses the POSIX API, pcre2posix.h will have to be
renamed or pointed at by a link (or the program modified, of course).
-If you are using the POSIX interface to PCRE2 and there is already a POSIX
-regex library installed on your system, as well as worrying about the regex.h
-header file (as mentioned above), you must also take care when linking programs
-to ensure that they link with PCRE2's libpcre2-posix library. Otherwise they
-may pick up the POSIX functions of the same name from the other library.
-
-To help with this issue, the libpcre2-posix library provides alternative names
-for the POSIX functions. These are the POSIX names, prefixed with "pcre2_", for
-example, pcre2_regcomp(). If an application can be compiled to use the
-alternative names (for example by the use of -Dregcomp=pcre2_regcomp etc.) it
-can be sure of linking with the PCRE2 functions.
-
Documentation for PCRE2
-----------------------
diff --git a/src/pcre2posix.c b/src/pcre2posix.c
index 3666100..1f382fa 100644
--- a/src/pcre2posix.c
+++ b/src/pcre2posix.c
@@ -177,24 +177,31 @@ static const char *const pstring[] = {
* Wrappers with traditional POSIX names *
*************************************************/
+/* Keep defining them to preseve ABI with application linked to pcre2-posix
+ * library before they were changed into macros. */
+
+#undef regerror
PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
{
return pcre2_regerror(errcode, preg, errbuf, errbuf_size);
}
+#undef regfree
PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION
regfree(regex_t *preg)
{
pcre2_regfree(preg);
}
+#undef regcomp
PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
regcomp(regex_t *preg, const char *pattern, int cflags)
{
return pcre2_regcomp(preg, pattern, cflags);
}
+#undef regexec
PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags)
diff --git a/src/pcre2posix.h b/src/pcre2posix.h
index bfe347e..b296df9 100644
--- a/src/pcre2posix.h
+++ b/src/pcre2posix.h
@@ -139,22 +139,23 @@ file. */
#endif
/* The functions. The actual code is in functions with pcre2_xxx names for
-uniqueness. Wrappers with the POSIX names are provided for those who can ensure
-they get them from the PCRE2 library and not by accident from elsewhere. */
+uniqueness. POSIX names are provided for API compatibility with POSIX regex
+functions. It's done this way to ensure to they are always get from the
+PCRE2 library and not by accident from elsewhere. (regex_t differs in size
+elsewhere.) */
PCRE2POSIX_EXP_DECL int pcre2_regcomp(regex_t *, const char *, int);
-PCRE2POSIX_EXP_DECL int regcomp(regex_t *, const char *, int);
+#define regcomp pcre2_regcomp
PCRE2POSIX_EXP_DECL int pcre2_regexec(const regex_t *, const char *, size_t,
regmatch_t *, int);
-PCRE2POSIX_EXP_DECL int regexec(const regex_t *, const char *, size_t,
- regmatch_t *, int);
+#define regexec pcre2_regexec
PCRE2POSIX_EXP_DECL size_t pcre2_regerror(int, const regex_t *, char *, size_t);
-PCRE2POSIX_EXP_DECL size_t regerror(int, const regex_t *, char *, size_t);
+#define regerror pcre2_regerror
PCRE2POSIX_EXP_DECL void pcre2_regfree(regex_t *);
-PCRE2POSIX_EXP_DECL void regfree(regex_t *);
+#define regfree pcre2_regfree
#ifdef __cplusplus
} /* extern "C" */
--
2.17.2

View File

@ -0,0 +1,278 @@
From 700b88ca0a5ef116431376d23e52bcfa71863b70 Mon Sep 17 00:00:00 2001
From: ph10 <ph10@6239d852-aaf2-0410-a92c-79f79f948069>
Date: Wed, 19 Sep 2018 16:33:09 +0000
Subject: [PATCH] Provide alternative POSIX names.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
git-svn-id: svn://vcs.exim.org/pcre2/code/trunk@1013 6239d852-aaf2-0410-a92c-79f79f948069
Petr Písař: Ported to 10.32.
---
README | 27 +++++++++++++--------------
doc/pcre2posix.3 | 24 +++++++++++++++++++++---
src/pcre2posix.c | 46 +++++++++++++++++++++++++++++++++++++++-------
src/pcre2posix.h | 18 +++++++++++++++---
diff --git a/README b/README
index 2eb621b..ae8adf8 100644
--- a/README
+++ b/README
@@ -42,18 +42,18 @@ the 16-bit library, which processes strings of 16-bit values, and one for the
32-bit library, which processes strings of 32-bit values. There are no C++
wrappers.
-The distribution does contain a set of C wrapper functions for the 8-bit
-library that are based on the POSIX regular expression API (see the pcre2posix
-man page). These can be found in a library called libpcre2-posix. Note that
-this just provides a POSIX calling interface to PCRE2; the regular expressions
-themselves still follow Perl syntax and semantics. The POSIX API is restricted,
-and does not give full access to all of PCRE2's facilities.
+In addition, the distribution contains a set of C wrapper functions for the
+8-bit library that are based on the POSIX regular expression API (see the
+pcre2posix man page). These are built into a library called libpcre2-posix.
+Note that this just provides a POSIX calling interface to PCRE2; the regular
+expressions themselves still follow Perl syntax and semantics. The POSIX API is
+restricted, and does not give full access to all of PCRE2's facilities.
The header file for the POSIX-style functions is called pcre2posix.h. The
official POSIX name is regex.h, but I did not want to risk possible problems
with existing files of that name by distributing it that way. To use PCRE2 with
an existing program that uses the POSIX API, pcre2posix.h will have to be
-renamed or pointed at by a link.
+renamed or pointed at by a link (or the program modified, of course).
If you are using the POSIX interface to PCRE2 and there is already a POSIX
regex library installed on your system, as well as worrying about the regex.h
@@ -61,12 +61,11 @@ header file (as mentioned above), you must also take care when linking programs
to ensure that they link with PCRE2's libpcre2-posix library. Otherwise they
may pick up the POSIX functions of the same name from the other library.
-One way of avoiding this confusion is to compile PCRE2 with the addition of
--Dregcomp=PCRE2regcomp (and similarly for the other POSIX functions) to the
-compiler flags (CFLAGS if you are using "configure" -- see below). This has the
-effect of renaming the functions so that the names no longer clash. Of course,
-you have to do the same thing for your applications, or write them using the
-new names.
+To help with this issue, the libpcre2-posix library provides alternative names
+for the POSIX functions. These are the POSIX names, prefixed with "pcre2_", for
+example, pcre2_regcomp(). If an application can be compiled to use the
+alternative names (for example by the use of -Dregcomp=pcre2_regcomp etc.) it
+can be sure of linking with the PCRE2 functions.
Documentation for PCRE2
@@ -888,4 +887,4 @@ The distribution should contain the files listed below.
Philip Hazel
Email local part: ph10
Email domain: cam.ac.uk
-Last updated: 17 June 2018
+Last updated: 19 September 2018
diff --git a/doc/pcre2posix.3 b/doc/pcre2posix.3
index 0d8b2c2..a91ccbf 100644
--- a/doc/pcre2posix.3
+++ b/doc/pcre2posix.3
@@ -1,4 +1,4 @@
-.TH PCRE2POSIX 3 "15 June 2017" "PCRE2 10.30"
+.TH PCRE2POSIX 3 "19 September 2018" "PCRE2 10.33"
.SH NAME
PCRE2 - Perl-compatible regular expressions (revised API)
.SH "SYNOPSIS"
@@ -10,13 +10,24 @@ PCRE2 - Perl-compatible regular expressions (revised API)
.B int regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP,
.B " int \fIcflags\fP);"
.sp
+.B int pcre2_regcomp(regex_t *\fIpreg\fP, const char *\fIpattern\fP,
+.B " int \fIcflags\fP);"
+.sp
.B int regexec(const regex_t *\fIpreg\fP, const char *\fIstring\fP,
.B " size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP);"
.sp
+.B int pcre2_regexec(const regex_t *\fIpreg\fP, const char *\fIstring\fP,
+.B " size_t \fInmatch\fP, regmatch_t \fIpmatch\fP[], int \fIeflags\fP);"
+.sp
.B "size_t regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP,"
.B " char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP);"
.sp
+.B "size_t pcre2_regerror(int \fIerrcode\fP, const regex_t *\fIpreg\fP,"
+.B " char *\fIerrbuf\fP, size_t \fIerrbuf_size\fP);"
+.sp
.B void regfree(regex_t *\fIpreg\fP);
+.sp
+.B void pcre2_regfree(regex_t *\fIpreg\fP);
.fi
.
.SH DESCRIPTION
@@ -38,6 +49,13 @@ header file, and on Unix systems the library itself is called
command for linking an application that uses them. Because the POSIX functions
call the native ones, it is also necessary to add \fB-lpcre2-8\fP.
.P
+When another POSIX regex library is also installed, there is the possibility of
+linking an application with the wrong library. To help avoid this issue, the
+PCRE2 POSIX library provides alternative names for the functions, all starting
+with "pcre2_". If an application uses these names, possible ambiguity is
+avoided. In the following description, however, the standard POSIX function
+names are used.
+.P
Those POSIX option bits that can reasonably be mapped to PCRE2 native options
have been implemented. In addition, the option REG_EXTENDED is defined with the
value zero. This has no effect, but since programs that are written to the
@@ -300,6 +318,6 @@ Cambridge, England.
.rs
.sp
.nf
-Last updated: 15 June 2017
-Copyright (c) 1997-2017 University of Cambridge.
+Last updated: 19 September 2018
+Copyright (c) 1997-2018 University of Cambridge.
.fi
diff --git a/src/pcre2posix.c b/src/pcre2posix.c
index 7b9f477..3666100 100644
--- a/src/pcre2posix.c
+++ b/src/pcre2posix.c
@@ -40,7 +40,10 @@ POSSIBILITY OF SUCH DAMAGE.
/* This module is a wrapper that provides a POSIX API to the underlying PCRE2
-functions. */
+functions. The operative functions are called pcre2_regcomp(), etc., with
+wrappers that use the plain POSIX names. This makes it easier for an
+application to be sure it gets the PCRE2 versions in the presence of other
+POSIX regex libraries. */
#ifdef HAVE_CONFIG_H
@@ -170,13 +173,44 @@ static const char *const pstring[] = {
+/*************************************************
+* Wrappers with traditional POSIX names *
+*************************************************/
+
+PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION
+regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
+{
+return pcre2_regerror(errcode, preg, errbuf, errbuf_size);
+}
+
+PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION
+regfree(regex_t *preg)
+{
+pcre2_regfree(preg);
+}
+
+PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
+regcomp(regex_t *preg, const char *pattern, int cflags)
+{
+return pcre2_regcomp(preg, pattern, cflags);
+}
+
+PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
+regexec(const regex_t *preg, const char *string, size_t nmatch,
+ regmatch_t pmatch[], int eflags)
+{
+return pcre2_regexec(preg, string, nmatch, pmatch, eflags);
+}
+
+
/*************************************************
* Translate error code to string *
*************************************************/
PCRE2POSIX_EXP_DEFN size_t PCRE2_CALL_CONVENTION
-regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
+pcre2_regerror(int errcode, const regex_t *preg, char *errbuf,
+ size_t errbuf_size)
{
int used;
const char *message;
@@ -199,13 +233,12 @@ return used + 1;
-
/*************************************************
* Free store held by a regex *
*************************************************/
PCRE2POSIX_EXP_DEFN void PCRE2_CALL_CONVENTION
-regfree(regex_t *preg)
+pcre2_regfree(regex_t *preg)
{
pcre2_match_data_free(preg->re_match_data);
pcre2_code_free(preg->re_pcre2_code);
@@ -213,7 +246,6 @@ pcre2_code_free(preg->re_pcre2_code);
-
/*************************************************
* Compile a regular expression *
*************************************************/
@@ -229,7 +261,7 @@ Returns: 0 on success
*/
PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
-regcomp(regex_t *preg, const char *pattern, int cflags)
+pcre2_regcomp(regex_t *preg, const char *pattern, int cflags)
{
PCRE2_SIZE erroffset;
PCRE2_SIZE patlen;
@@ -296,7 +328,7 @@ for each match. If REG_NOSUB was specified at compile time, the nmatch and
pmatch arguments are ignored, and the only result is yes/no/error. */
PCRE2POSIX_EXP_DEFN int PCRE2_CALL_CONVENTION
-regexec(const regex_t *preg, const char *string, size_t nmatch,
+pcre2_regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags)
{
int rc, so, eo;
diff --git a/src/pcre2posix.h b/src/pcre2posix.h
index 4ae1d3c..bfe347e 100644
--- a/src/pcre2posix.h
+++ b/src/pcre2posix.h
@@ -3,11 +3,13 @@
*************************************************/
/* PCRE2 is a library of functions to support regular expressions whose syntax
-and semantics are as close as possible to those of the Perl 5 language.
+and semantics are as close as possible to those of the Perl 5 language. This is
+the public header file to be #included by applications that call PCRE2 via the
+POSIX wrapper interface.
Written by Philip Hazel
Original API code Copyright (c) 1997-2012 University of Cambridge
- New API code Copyright (c) 2016 University of Cambridge
+ New API code Copyright (c) 2016-2018 University of Cambridge
-----------------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without
@@ -136,12 +138,22 @@ file. */
# endif
#endif
-/* The functions */
+/* The functions. The actual code is in functions with pcre2_xxx names for
+uniqueness. Wrappers with the POSIX names are provided for those who can ensure
+they get them from the PCRE2 library and not by accident from elsewhere. */
+PCRE2POSIX_EXP_DECL int pcre2_regcomp(regex_t *, const char *, int);
PCRE2POSIX_EXP_DECL int regcomp(regex_t *, const char *, int);
+
+PCRE2POSIX_EXP_DECL int pcre2_regexec(const regex_t *, const char *, size_t,
+ regmatch_t *, int);
PCRE2POSIX_EXP_DECL int regexec(const regex_t *, const char *, size_t,
regmatch_t *, int);
+
+PCRE2POSIX_EXP_DECL size_t pcre2_regerror(int, const regex_t *, char *, size_t);
PCRE2POSIX_EXP_DECL size_t regerror(int, const regex_t *, char *, size_t);
+
+PCRE2POSIX_EXP_DECL void pcre2_regfree(regex_t *);
PCRE2POSIX_EXP_DECL void regfree(regex_t *);
#ifdef __cplusplus
--
2.17.2

View File

@ -9,7 +9,7 @@
#%%global rcversion RC1 #%%global rcversion RC1
Name: pcre2 Name: pcre2
Version: 10.32 Version: 10.32
Release: %{?rcversion:0.}5%{?rcversion:.%rcversion}%{?dist} Release: %{?rcversion:0.}6%{?rcversion:.%rcversion}%{?dist}
%global myversion %{version}%{?rcversion:-%rcversion} %global myversion %{version}%{?rcversion:-%rcversion}
Summary: Perl-compatible regular expression library Summary: Perl-compatible regular expression library
# the library: BSD with exceptions # the library: BSD with exceptions
@ -68,6 +68,12 @@ Patch5: pcre2-10.32-Fix-non-recognition-of-anchoring-when-preceded-by-MA.pat
# fix an undefined behavior in aarch64 JIT compiler, upstream bug #2355, # fix an undefined behavior in aarch64 JIT compiler, upstream bug #2355,
# in upstream after 10.32 # in upstream after 10.32
Patch6: pcre2-10.32-JIT-compiler-update.patch Patch6: pcre2-10.32-JIT-compiler-update.patch
# Define PCRE2-specific symbols in pcre2-posix library, bug #1667614,
# upstream bug 1830, in upstream after 10.32
Patch7: pcre2-10.32-Provide-alternative-POSIX-names.patch
# Link applications to PCRE2-specific symbols when using POSIX API, bug #1667614,
# upstream bug 1830, proposed to upstream
Patch8: pcre2-10.32-Declare-POSIX-regex-function-names-as-macros-to-PCRE.patch
BuildRequires: autoconf BuildRequires: autoconf
BuildRequires: automake BuildRequires: automake
BuildRequires: coreutils BuildRequires: coreutils
@ -150,6 +156,8 @@ Utilities demonstrating PCRE2 capabilities like pcre2grep or pcre2test.
%patch4 -p1 %patch4 -p1
%patch5 -p1 %patch5 -p1
%patch6 -p1 %patch6 -p1
%patch7 -p1
%patch8 -p1
# Because of multilib patch # Because of multilib patch
libtoolize --copy --force libtoolize --copy --force
autoreconf -vif autoreconf -vif
@ -251,6 +259,9 @@ make %{?_smp_mflags} check VERBOSE=yes
%{_mandir}/man1/pcre2test.* %{_mandir}/man1/pcre2test.*
%changelog %changelog
* Tue Jan 22 2019 Petr Pisar <ppisar@redhat.com> - 10.32-6
- Link applications to PCRE2-specific symbols when using POSIX API (bug #1667614)
* Thu Jan 03 2019 Petr Pisar <ppisar@redhat.com> - 10.32-5 * Thu Jan 03 2019 Petr Pisar <ppisar@redhat.com> - 10.32-5
- Fix anchoring a pattern preceded with (*MARK) - Fix anchoring a pattern preceded with (*MARK)
- Fix OpenPOWER 64-bit ELFv2 ABI detection in JIT compiler (upstream bug #2353) - Fix OpenPOWER 64-bit ELFv2 ABI detection in JIT compiler (upstream bug #2353)