Patchwork lib: fwts_klog: Don't regex compile non-regex strings

login
register
mail settings
Submitter Colin King
Date Nov. 22, 2012, 2:52 p.m.
Message ID <1353595950-27888-1-git-send-email-colin.king@canonical.com>
Download mbox | patch
Permalink /patch/201060/
State Accepted
Headers show

Comments

Colin King - Nov. 22, 2012, 2:52 p.m.
From: Colin Ian King <colin.king@canonical.com>

For some reason that I overlooked, I'm compiling non-regex strings
which is plainly wrong. Fix this - just regex compile regex patterns
and unsure the .re and .extra fields are NULL on error conditions and
for non-regex expressions to bullet proof the code.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_klog.c | 21 ++++++++++++++-------
 1 file changed, 14 insertions(+), 7 deletions(-)
Keng-Yu Lin - Nov. 27, 2012, 8:08 a.m.
On Thu, Nov 22, 2012 at 10:52 PM, Colin King <colin.king@canonical.com> wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> For some reason that I overlooked, I'm compiling non-regex strings
> which is plainly wrong. Fix this - just regex compile regex patterns
> and unsure the .re and .extra fields are NULL on error conditions and
> for non-regex expressions to bullet proof the code.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/src/fwts_klog.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
> index 54beae2..0a6350c 100644
> --- a/src/lib/src/fwts_klog.c
> +++ b/src/lib/src/fwts_klog.c
> @@ -397,15 +397,22 @@ static int fwts_klog_check(fwts_framework *fw,
>                 if (patterns[i].label == NULL)
>                         goto fail;
>
> -               if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> -                       fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
> -                       patterns[i].re = NULL;
> -               } else {
> -                       patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> -                       if (error != NULL) {
> -                               fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
> +               if (patterns[i].compare_mode == FWTS_COMPARE_REGEX) {
> +                       if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> +                               fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
>                                 patterns[i].re = NULL;
> +                               patterns[i].extra = NULL;
> +                       } else {
> +                               patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> +                               if (error != NULL) {
> +                                       fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
> +                                       patterns[i].re = NULL;
> +                                       patterns[i].extra = NULL;
> +                               }
>                         }
> +               } else {
> +                       patterns[i].re = NULL;
> +                       patterns[i].extra = NULL;
>                 }
>         }
>         /* We've now collected up the scan patterns, lets scan the log for errors */
> --
> 1.8.0
>
Acked-by: Keng-Yu Lin <kengyu@canonical.com>
Ivan Hu - Nov. 28, 2012, 9:06 a.m.
On 11/22/2012 10:52 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> For some reason that I overlooked, I'm compiling non-regex strings
> which is plainly wrong. Fix this - just regex compile regex patterns
> and unsure the .re and .extra fields are NULL on error conditions and
> for non-regex expressions to bullet proof the code.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_klog.c | 21 ++++++++++++++-------
>   1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
> index 54beae2..0a6350c 100644
> --- a/src/lib/src/fwts_klog.c
> +++ b/src/lib/src/fwts_klog.c
> @@ -397,15 +397,22 @@ static int fwts_klog_check(fwts_framework *fw,
>   		if (patterns[i].label == NULL)
>   			goto fail;
>
> -		if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> -			fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
> -			patterns[i].re = NULL;
> -		} else {
> -			patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> -			if (error != NULL) {
> -				fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
> +		if (patterns[i].compare_mode == FWTS_COMPARE_REGEX) {
> +			if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
> +				fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
>   				patterns[i].re = NULL;
> +				patterns[i].extra = NULL;
> +			} else {
> +				patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
> +				if (error != NULL) {
> +					fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
> +					patterns[i].re = NULL;
> +					patterns[i].extra = NULL;
> +				}
>   			}
> +		} else {
> +			patterns[i].re = NULL;
> +			patterns[i].extra = NULL;
>   		}
>   	}
>   	/* We've now collected up the scan patterns, lets scan the log for errors */
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>

Patch

diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
index 54beae2..0a6350c 100644
--- a/src/lib/src/fwts_klog.c
+++ b/src/lib/src/fwts_klog.c
@@ -397,15 +397,22 @@  static int fwts_klog_check(fwts_framework *fw,
 		if (patterns[i].label == NULL)
 			goto fail;
 
-		if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
-			fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
-			patterns[i].re = NULL;
-		} else {
-			patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
-			if (error != NULL) {
-				fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
+		if (patterns[i].compare_mode == FWTS_COMPARE_REGEX) {
+			if ((patterns[i].re = pcre_compile(patterns[i].pattern, 0, &error, &erroffset, NULL)) == NULL) {
+				fwts_log_error(fw, "Regex %s failed to compile: %s.", patterns[i].pattern, error);
 				patterns[i].re = NULL;
+				patterns[i].extra = NULL;
+			} else {
+				patterns[i].extra = pcre_study(patterns[i].re, 0, &error);
+				if (error != NULL) {
+					fwts_log_error(fw, "Regex %s failed to optimize: %s.", patterns[i].pattern, error);
+					patterns[i].re = NULL;
+					patterns[i].extra = NULL;
+				}
 			}
+		} else {
+			patterns[i].re = NULL;
+			patterns[i].extra = NULL;
 		}
 	}
 	/* We've now collected up the scan patterns, lets scan the log for errors */