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

Alex Hung alex.hung at canonical.com
Wed Nov 21 05:51:10 UTC 2012


On 11/03/2012 12:58 AM, Colin King 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;
>
Acked-by: Alex Hung <alex.hung at canonical.com>



More information about the fwts-devel mailing list