ACK: [PATCH 2/2] utilities: kernelscan: remove use of pcre, use regex instead

Alex Hung alex.hung at canonical.com
Wed Nov 25 03:41:53 UTC 2015


On 2015-11-24 09:01 PM, Colin King wrote:
> From: Colin Ian King <colin.king at canonical.com>
>
> Using pcre does not offer any extra benefit over regex and
> requires an extra library dependency.  Remove pcre and replace
> with regex.
>
> Signed-off-by: Colin Ian King <colin.king at canonical.com>
> ---
>   src/utilities/Makefile.am  |  1 -
>   src/utilities/kernelscan.c | 34 +++++++++++++++-------------------
>   2 files changed, 15 insertions(+), 20 deletions(-)
>
> diff --git a/src/utilities/Makefile.am b/src/utilities/Makefile.am
> index 3160a35..9a47980 100644
> --- a/src/utilities/Makefile.am
> +++ b/src/utilities/Makefile.am
> @@ -4,5 +4,4 @@ AM_CPPFLAGS = -Wall -Werror -Wextra -DDATAROOTDIR=\"$(datarootdir)\" \
>
>   bin_PROGRAMS = kernelscan
>   kernelscan_SOURCES = kernelscan.c
> -kernelscan_LDFLAGS = -lpcre
>
> diff --git a/src/utilities/kernelscan.c b/src/utilities/kernelscan.c
> index 9dca440..10b51d6 100644
> --- a/src/utilities/kernelscan.c
> +++ b/src/utilities/kernelscan.c
> @@ -23,9 +23,10 @@
>   #include <string.h>
>   #include <ctype.h>
>   #include <unistd.h>
> -
> -#include <pcre.h>
> +#include <sys/types.h>
> +#include <regex.h>
>   #include <json.h>
> +
>   #include "config.h"
>
>   #define PARSER_OK		0
> @@ -49,8 +50,8 @@ typedef enum {
>   typedef struct {
>           char *pattern;		/* pattern that we compare to kernel messages */
>   	compare_mode cm;	/* 'r' regex or 's' string comparison */
> -        pcre *re;		/* regex from pattern */
> -        pcre_extra *extra;
> +        regex_t compiled;	/* regex from pattern */
> +	bool compiled_ok;	/* regex compiled OK */
>   } klog_pattern;
>
>   /*
> @@ -270,9 +271,7 @@ static klog_pattern *klog_load(const char *table)
>
>   	/* Now fetch json objects and compile regex */
>   	for (i = 0; i < n; i++) {
> -		const char *error;
>   		const char *str;
> -		int erroffset;
>   		json_object *obj;
>   #if JSON_HAS_GET_EX
>   		json_object *obj_str;
> @@ -320,15 +319,14 @@ static klog_pattern *klog_load(const char *table)
>
>   		/* 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;
> +			int rc;
> +
> +			rc = regcomp(&patterns[i].compiled, patterns[i].pattern, REG_EXTENDED);
> +			if (rc) {
> +				fprintf(stderr, "Regex %s failed to compile: %d.\n", patterns[i].pattern, rc);
> +				patterns[i].compiled_ok = false;
>   			} 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;
> -				}
> +				patterns[i].compiled_ok = true;
>   			}
>   		}
>   	}
> @@ -352,10 +350,9 @@ static bool klog_find(char *str, klog_pattern *patterns)
>   				return true;
>   			}
>   		} 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) {
> +			if (patterns[i].compiled_ok &&
> +			    (!regexec(&patterns[i].compiled, str, 0, NULL, 0)))
>   				return true;
> -			}
>   		}
>   	}
>
> @@ -370,8 +367,7 @@ static void klog_free(klog_pattern *patterns)
>   	int i;
>
>   	for (i = 0; patterns[i].pattern; i++) {
> -		pcre_free(patterns[i].re);
> -		pcre_free(patterns[i].extra);
> +		regfree(&patterns[i].compiled);
>   		free(patterns[i].pattern);
>   	}
>   	free(patterns);
>

Acked-by: Alex Hung <alex.hung at canonical.com>



More information about the fwts-devel mailing list