]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
fix crash when no previous regex seen
authorMatthew Newton <matthew-git@newtoncomputing.co.uk>
Thu, 27 Feb 2020 13:54:59 +0000 (13:54 +0000)
committerMatthew Newton <matthew-git@newtoncomputing.co.uk>
Fri, 28 Feb 2020 01:30:09 +0000 (01:30 +0000)
src/lib/server/regex.c
src/lib/server/xlat_builtin.c
src/tests/keywords/if-regex-match-named

index d830b510a7037080b703e351c0df232b6f0322c7..b5c7bdd918ed466aec3922bf10e3352b73f745cd 100644 (file)
@@ -127,7 +127,7 @@ int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t
        if (!rc) {
                RDEBUG4("No subcapture data found");
                *out = NULL;
-               return 1;
+               return -1;
        }
        match_data = talloc_get_type_abort(rc->regmatch->match_data, pcre2_match_data);
 
@@ -198,7 +198,7 @@ int regex_request_to_sub_named(TALLOC_CTX *ctx, char **out, REQUEST *request, ch
        if (!rc) {
                RDEBUG4("No subcapture data found");
                *out = NULL;
-               return 1;
+               return -1;
        }
        match_data = rc->regmatch->match_data;
 
@@ -266,7 +266,7 @@ int regex_request_to_sub(TALLOC_CTX *ctx, char **out, REQUEST *request, uint32_t
        if (!rc) {
                RDEBUG4("No subcapture data found");
                *out = NULL;
-               return 1;
+               return -1;
        }
 
        ret = pcre_get_substring(rc->regmatch->subject,
@@ -335,7 +335,7 @@ int regex_request_to_sub_named(TALLOC_CTX *ctx, char **out, REQUEST *request, ch
        if (!rc) {
                RDEBUG4("No subcapture data found");
                *out = NULL;
-               return 1;
+               return -1;
        }
 
        ret = pcre_get_named_substring(rc->preg->compiled, rc->regmatch->subject,
index f4c55a5bcf3e7605f31a5668ddf91b78dc9cf90f..1e1376a159d7a769135b689d63c251fd58666dd8 100644 (file)
@@ -2452,7 +2452,12 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                fr_value_box_t  *vb;
                char            *p;
 
-               if (regex_request_to_sub(ctx, &p, request, 0) < 0) return XLAT_ACTION_FAIL;
+               if (regex_request_to_sub(ctx, &p, request, 0) < 0) {
+                       REDEBUG2("No previous regex capture");
+                       return XLAT_ACTION_FAIL;
+               }
+
+               rad_assert(p);
 
                MEM(vb = fr_value_box_alloc_null(ctx));
                fr_value_box_bstrsteal(vb, vb, NULL, p, false);
@@ -2482,7 +2487,12 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                        return XLAT_ACTION_FAIL;
                }
 
-               if (regex_request_to_sub(ctx, &p, request, idx.vb_uint32) < 0) return XLAT_ACTION_FAIL;
+               if (regex_request_to_sub(ctx, &p, request, idx.vb_uint32) < 0) {
+                       REDEBUG2("No previous numbered regex capture group");
+                       return XLAT_ACTION_FAIL;
+               }
+
+               rad_assert(p);
 
                MEM(vb = fr_value_box_alloc_null(ctx));
                fr_value_box_bstrsteal(vb, vb, NULL, p, false);
@@ -2504,7 +2514,12 @@ static xlat_action_t xlat_func_regex(TALLOC_CTX *ctx, fr_cursor_t *out,
                        return XLAT_ACTION_FAIL;
                }
 
-               if (regex_request_to_sub_named(request, &p, request, (*in)->vb_strvalue) < 0) return XLAT_ACTION_FAIL;
+               if (regex_request_to_sub_named(request, &p, request, (*in)->vb_strvalue) < 0) {
+                       REDEBUG2("No previous named regex capture group");
+                       return XLAT_ACTION_FAIL;
+               }
+
+               rad_assert(p);
 
                MEM(vb = fr_value_box_alloc_null(ctx));
                fr_value_box_bstrsteal(vb, vb, NULL, p, false);
index 6a8ef249e6c7208d421d18478ac27e16e73edf19..5184e991bfbfab8d35a04c6b55b2cca9ac002be3 100644 (file)
@@ -3,6 +3,46 @@
 if (('${feature.regex-pcre}' == 'yes') || ('${feature.regex-pcre2}' == 'yes')) {
 update request {
        &Tmp-Integer-0 := '123456789'
+       &Tmp-Integer-1 := 1
+}
+
+# Check failure when no previous capture - full capture
+if ("%{regex:}" != "") {
+       test_fail
+}
+
+if (&Module-Failure-Message[*] != "No previous regex capture") {
+       test_fail
+}
+
+update request {
+       &Module-Failure-Message !* ANY
+}
+
+# Check failure when no previous capture - named group
+if ("%{regex:foo}" != "") {
+       test_fail
+}
+
+if (&Module-Failure-Message[*] != "No previous named regex capture group") {
+       test_fail
+}
+
+update request {
+       &Module-Failure-Message !* ANY
+}
+
+# Check failure when no previous capture - numbered group
+if ("%{regex:%{Tmp-Integer-1}}" != "") {
+       test_fail
+}
+
+if (&Module-Failure-Message[*] != "No previous numbered regex capture group") {
+       test_fail
+}
+
+update request {
+       &Module-Failure-Message !* ANY
 }
 
 # uncompiled - ref - named capture groups