4e41214
From f1e9a32ee7fad2263636a51536ce0f9f13f09949 Mon Sep 17 00:00:00 2001
4e41214
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
4e41214
Date: Wed, 23 Jan 2019 10:16:20 +0100
4e41214
Subject: [PATCH] Declare POSIX regex function names as macros to PCRE
4e41214
 functions
4e41214
MIME-Version: 1.0
4e41214
Content-Type: text/plain; charset=UTF-8
4e41214
Content-Transfer-Encoding: 8bit
4e41214
4e41214
POSIX regex libraries differ in regex_t size. If a program includes
4e41214
<pcreposix.h>, but is not linked to pcreposix library at run-time
4e41214
(either in effect of --as-needed or a lazy binding in dlopen)
4e41214
other implementation touches memory out of the structure and the
4e41214
program can crash.
4e41214
4e41214
That means once a program includes <pcreposix.h>, it must link to the
4e41214
pcreposix library.
4e41214
4e41214
This patch replaces the POSIX regex declaration with macros to the
4e41214
PCRE uniqely-named function. This ensures that the PCRE's regex_t
4e41214
structure is always handled by the PCRE functions.
4e41214
4e41214
This patch still preserves the POSIX regex definitions in order to
4e41214
preseve ABI with application compiled before this change. The
4e41214
definition can be removed in the future.
4e41214
4e41214
Signed-off-by: Petr Písař <ppisar@redhat.com>
4e41214
---
4e41214
 pcreposix.c | 50 +++++++++++++++++++++++++++++++++++++++++++++-----
4e41214
 pcreposix.h | 20 ++++++++++++++------
4e41214
 2 files changed, 59 insertions(+), 11 deletions(-)
4e41214
4e41214
diff --git a/pcreposix.c b/pcreposix.c
4e41214
index a76d6bf..3f2f3ef 100644
4e41214
--- a/pcreposix.c
4e41214
+++ b/pcreposix.c
4e41214
@@ -39,7 +39,10 @@ POSSIBILITY OF SUCH DAMAGE.
4e41214
 
4e41214
 
4e41214
 /* This module is a wrapper that provides a POSIX API to the underlying PCRE
4e41214
-functions. */
4e41214
+functions. The operative functions are called pcre_regcomp(), etc., with
4e41214
+wrappers that use the plain POSIX names. This makes it easier for an
4e41214
+application to be sure it gets the PCRE versions in the presence of other
4e41214
+POSIX regex libraries. */
4e41214
 
4e41214
 
4e41214
 #ifdef HAVE_CONFIG_H
4e41214
@@ -204,12 +207,49 @@ static const char *const pstring[] = {
4e41214
 
4e41214
 
4e41214
 /*************************************************
4e41214
-*          Translate error code to string        *
4e41214
+*      Wrappers with traditional POSIX names     *
4e41214
 *************************************************/
4e41214
 
4e41214
+/* Keep defining them to preseve ABI with application linked to pcreposix
4e41214
+ * library before they were changed into macros. */
4e41214
+
4e41214
+#undef regerror
4e41214
 PCREPOSIX_EXP_DEFN size_t PCRE_CALL_CONVENTION
4e41214
 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
4e41214
 {
4e41214
+return pcre_regerror(errcode, preg, errbuf, errbuf_size);
4e41214
+}
4e41214
+
4e41214
+#undef regfree
4e41214
+PCREPOSIX_EXP_DEFN void PCRE_CALL_CONVENTION
4e41214
+regfree(regex_t *preg)
4e41214
+{
4e41214
+pcre_regfree(preg);
4e41214
+}
4e41214
+
4e41214
+#undef regcomp
4e41214
+PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
4e41214
+regcomp(regex_t *preg, const char *pattern, int cflags)
4e41214
+{
4e41214
+return pcre_regcomp(preg, pattern, cflags);
4e41214
+}
4e41214
+
4e41214
+#undef regexec
4e41214
+PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
4e41214
+regexec(const regex_t *preg, const char *string, size_t nmatch,
4e41214
+  regmatch_t pmatch[], int eflags)
4e41214
+{
4e41214
+return pcre_regexec(preg, string, nmatch, pmatch, eflags);
4e41214
+}
4e41214
+
4e41214
+
4e41214
+/*************************************************
4e41214
+*          Translate error code to string        *
4e41214
+*************************************************/
4e41214
+
4e41214
+PCREPOSIX_EXP_DEFN size_t PCRE_CALL_CONVENTION
4e41214
+pcre_regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
4e41214
+{
4e41214
 const char *message, *addmessage;
4e41214
 size_t length, addlength;
4e41214
 
4e41214
@@ -243,7 +283,7 @@ return length + addlength;
4e41214
 *************************************************/
4e41214
 
4e41214
 PCREPOSIX_EXP_DEFN void PCRE_CALL_CONVENTION
4e41214
-regfree(regex_t *preg)
4e41214
+pcre_regfree(regex_t *preg)
4e41214
 {
4e41214
 (PUBL(free))(preg->re_pcre);
4e41214
 }
4e41214
@@ -266,7 +306,7 @@ Returns:      0 on success
4e41214
 */
4e41214
 
4e41214
 PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
4e41214
-regcomp(regex_t *preg, const char *pattern, int cflags)
4e41214
+pcre_regcomp(regex_t *preg, const char *pattern, int cflags)
4e41214
 {
4e41214
 const char *errorptr;
4e41214
 int erroffset;
4e41214
@@ -320,7 +360,7 @@ be set. When this is the case, the nmatch and pmatch arguments are ignored, and
4e41214
 the only result is yes/no/error. */
4e41214
 
4e41214
 PCREPOSIX_EXP_DEFN int PCRE_CALL_CONVENTION
4e41214
-regexec(const regex_t *preg, const char *string, size_t nmatch,
4e41214
+pcre_regexec(const regex_t *preg, const char *string, size_t nmatch,
4e41214
   regmatch_t pmatch[], int eflags)
4e41214
 {
4e41214
 int rc, so, eo;
4e41214
diff --git a/pcreposix.h b/pcreposix.h
4e41214
index c77c0b0..6f108b8 100644
4e41214
--- a/pcreposix.h
4e41214
+++ b/pcreposix.h
4e41214
@@ -131,13 +131,21 @@ file. */
4e41214
 #  endif
4e41214
 #endif
4e41214
 
4e41214
-/* The functions */
4e41214
-
4e41214
-PCREPOSIX_EXP_DECL int regcomp(regex_t *, const char *, int);
4e41214
-PCREPOSIX_EXP_DECL int regexec(const regex_t *, const char *, size_t,
4e41214
+/* The functions. The actual code is in functions with pcre_xxx names for
4e41214
+uniqueness. POSIX names are provided for API compatibility with POSIX regex
4e41214
+functions. It's done this way to ensure to they are always get from the
4e41214
+PCRE library and not by accident from elsewhere. (regex_t differs in size
4e41214
+elsewhere.) */
4e41214
+
4e41214
+PCREPOSIX_EXP_DECL int pcre_regcomp(regex_t *, const char *, int);
4e41214
+#define regcomp pcre_regcomp
4e41214
+PCREPOSIX_EXP_DECL int pcre_regexec(const regex_t *, const char *, size_t,
4e41214
                      regmatch_t *, int);
4e41214
-PCREPOSIX_EXP_DECL size_t regerror(int, const regex_t *, char *, size_t);
4e41214
-PCREPOSIX_EXP_DECL void regfree(regex_t *);
4e41214
+#define regexec pcre_regexec
4e41214
+PCREPOSIX_EXP_DECL size_t pcre_regerror(int, const regex_t *, char *, size_t);
4e41214
+#define regerror pcre_regerror
4e41214
+PCREPOSIX_EXP_DECL void pcre_regfree(regex_t *);
4e41214
+#define regfree pcre_regfree
4e41214
 
4e41214
 #ifdef __cplusplus
4e41214
 }   /* extern "C" */
4e41214
-- 
4e41214
2.17.2
4e41214