[PATCH 4/6] utlities: kernelscan: make some regex optimisations

Keng-Yu Lin kengyu at canonical.com
Wed Nov 7 02:19:02 UTC 2012


On Sat, Nov 3, 2012 at 12:58 AM, Colin King <colin.king at canonical.com> wrote:
> From: Colin Ian King <colin.king at canonical.com>
>
> We only should compile a pattern if it is a regular expression. Also,
> make the klog_find a little faster.
>
> Signed-off-by: Colin Ian King <colin.king at canonical.com>
> ---
>  src/utilities/kernelscan.c |   20 +++++++++++---------
>  1 file changed, 11 insertions(+), 9 deletions(-)
>
> diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
> index 9133ce9..2e0d606 100644
> --- a/src/utilities/kernelscan.c
> +++ b/src/utilities/kernelscan.c
> @@ -293,14 +293,17 @@ static klog_pattern *klog_load(const char *table)
>                         exit(EXIT_FAILURE);
>                 }
>
> -               if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> -                       fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
> -                       patterns[i].re = NULL;
> -               } else {
> -                       patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> -                       if (error != NULL) {
> -                               fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +               /* Pre-compile regular expressions to make things run a bit faster */
> +               if (patterns[i].cm == COMPARE_REGEX) {
> +                       if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> +                               fprintf(stderr, "Regex %s failed to compile: %s.\n", patterns[i].pattern, error);
>                                 patterns[i].re = NULL;
> +                       } else {
> +                               patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> +                               if (error != NULL) {
> +                                       fprintf(stderr, "Regex %s failed to optimize: %s.\n", patterns[i].pattern, error);
> +                                       patterns[i].re = NULL;
> +                               }
>                         }
>                 }
>         }
> @@ -323,8 +326,7 @@ static bool klog_find(char *str, klog_pattern *patterns)
>                         if (strstr(str, patterns[i].pattern)) {
>                                 return true;
>                         }
> -               }
> -               if (patterns[i].cm == COMPARE_REGEX) {
> +               } else if (patterns[i].cm == COMPARE_REGEX) {
>                         int vector[1];
>                         if (pcre_exec(patterns[i].re, patterns[i].extra, str, strlen(str), 0, 0, vector, 1) == 0) {
>                                 return true;
> --
> 1.7.10.4
>
Acked-by: Keng-Yu Lin <kengyu at canonical.com>



More information about the fwts-devel mailing list