]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
PCRE2_COPY_MATCHED_SUBJECT doesn't work for pcre2_jit_match apparently
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 19 Apr 2019 14:12:53 +0000 (10:12 -0400)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Fri, 19 Apr 2019 14:13:01 +0000 (10:13 -0400)
src/lib/server/regex.c
src/lib/util/regex.c
src/lib/util/regex.h

index fd325ce5839ef74a8a1a0ff92156726bec619555..6a3b3977e32a33ded4ec086645d220dda6efc242 100644 (file)
@@ -128,7 +128,7 @@ int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t
                *out = NULL;
                return 1;
        }
-       match_data = rc->regmatch->match_data;
+       match_data = talloc_get_type_abort(rc->regmatch->match_data, pcre2_match_data);
 
        ret = pcre2_substring_length_bynumber(match_data, num, &len);
        switch (ret) {
index 55d2325e574455b8262d4053727e94e03a62f38d..72b3fda1d5ffef48207f3537be22fba538039ba0 100644 (file)
@@ -284,9 +284,8 @@ int regex_exec(regex_t *preg, char const *subject, size_t len, fr_regmatch_t *re
        int                     ret;
        uint32_t                options = 0;
 
-#ifndef PCRE2_COPY_MATCHED_SUBJECT
        char                    *our_subject = NULL;
-#endif
+       bool                    dup_subject = true;
 
        /*
         *      Thread local initialisation
@@ -294,33 +293,45 @@ int regex_exec(regex_t *preg, char const *subject, size_t len, fr_regmatch_t *re
        if (!fr_pcre2_tls && (fr_pcre2_tls_init() < 0)) return -1;
 
        if (regmatch) {
-#ifndef PCRE2_COPY_MATCHED_SUBJECT
+#ifdef PCRE2_COPY_MATCHED_SUBJECT
                /*
-                *      We have to dup and operate on the duplicate
-                *      of the subject, because pcre2_jit_match and
-                *      pcre2_match store a pointer to the subject
-                *      in the regmatch structure.
+                *      This is apparently only supported for pcre2_match
+                *      NOT pcre2_jit_match.
                 */
-               subject = our_subject = talloc_bstrndup(regmatch, subject, len);
-               if (!subject) {
-                       fr_strerror_printf("Out of memory");
-                       return -1;
+               if (!preg->jitd) {
+                       dup_subject = false;
+
+                       /*
+                        *      If PCRE2_COPY_MATCHED_SUBJECT is available
+                        *      and set as an options flag, pcre2_match will
+                        *      strdup the subject string if pcre2_match is
+                        *      successful and store a pointer to it in the
+                        *      regmatch struct.
+                        *
+                        *      The lifetime of the string memory will be
+                        *      bound to the regmatch struct.  This is more
+                        *      efficient that doing it ourselves, as the
+                        *      strdup only occurs if the subject matches.
+                        */
+                       options |= PCRE2_COPY_MATCHED_SUBJECT;
                }
-#else
-               /*
-                *      If PCRE2_COPY_MATCHED_SUBJECT is available
-                *      and set as an options flag, pcre2_match will
-                *      strdup the subject string if pcre2_match is
-                *      successful and store a pointer to it in the
-                *      regmatch struct.
-                *
-                *      The lifetime of the string memory will be
-                *      bound to the regmatch struct.  This is more
-                *      efficient that doing it ourselves, as the
-                *      strdup only occurs if the subject matches.
-                */
-               options |= PCRE2_COPY_MATCHED_SUBJECT;
 #endif
+               if (dup_subject) {
+                       /*
+                        *      We have to dup and operate on the duplicate
+                        *      of the subject, because pcre2_jit_match and
+                        *      pcre2_match store a pointer to the subject
+                        *      in the regmatch structure.
+                        */
+                       subject = our_subject = talloc_bstrndup(regmatch, subject, len);
+                       if (!subject) {
+                               fr_strerror_printf("Out of memory");
+                               return -1;
+                       }
+#ifndef NDEBUG
+                       regmatch->subject = subject; /* Stored only for tracking memory issues */
+#endif
+               }
        }
 
        if (preg->jitd) {
@@ -333,9 +344,7 @@ int regex_exec(regex_t *preg, char const *subject, size_t len, fr_regmatch_t *re
        if (ret < 0) {
                PCRE2_UCHAR errbuff[128];
 
-#ifndef PCRE2_COPY_MATCHED_SUBJECT
-               talloc_free(our_subject);
-#endif
+               if (dup_subject) talloc_free(our_subject);
 
                if (ret == PCRE2_ERROR_NOMATCH) {
                        if (regmatch) regmatch->used = 0;
index 8e6aa6a923241e67b157f8a4eb1fad7775eaa556..212b35952ba84cdd8e3d5a64a572a0517a785a06 100644 (file)
@@ -55,6 +55,9 @@ typedef struct {
        pcre2_match_data        *match_data;    //!< Match data containing the subject
                                                ///< and various match offsets.
        size_t                  used;           //!< Number of slots filled with match data.
+#ifndef NDEBUG
+       char const              *subject;       //!< Here for debugging purposes if we explicitly duped the string.
+#endif
 } fr_regmatch_t;
 
 typedef struct {