diff mbox

lib: fwts_log: add LOG_NO_FIELD and use this to indicate an unfound field

Message ID 20170418220853.30618-1-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King April 18, 2017, 10:08 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

Rather than implicitly using a non-enum fwts_log_field value zero
to indicate an unfound field instead use a new explicitly defined
LOG_NO_FIELD enum value to indicate this.  This cleans up a couple
of static analysis warnings of the form:

"The variable 'field' is of enum type. It is odd that it is used as a
variable of a Boolean-type."

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/include/fwts_log.h | 1 +
 src/lib/src/fwts_log.c     | 6 +++---
 2 files changed, 4 insertions(+), 3 deletions(-)

Comments

Alex Hung April 18, 2017, 10:14 p.m. UTC | #1
On 2017-04-18 03:08 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Rather than implicitly using a non-enum fwts_log_field value zero
> to indicate an unfound field instead use a new explicitly defined
> LOG_NO_FIELD enum value to indicate this.  This cleans up a couple
> of static analysis warnings of the form:
>
> "The variable 'field' is of enum type. It is odd that it is used as a
> variable of a Boolean-type."
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_log.h | 1 +
>  src/lib/src/fwts_log.c     | 6 +++---
>  2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
> index 3c7dd4d3..028ba36c 100644
> --- a/src/lib/include/fwts_log.h
> +++ b/src/lib/include/fwts_log.h
> @@ -30,6 +30,7 @@
>  #define LOG_MAX_BUF_SIZE	(4096)		/* Max output per log line */
>
>  typedef enum {
> +	LOG_NO_FIELD        = 0x00000000,
>  	LOG_RESULT	    = 0x00000001,
>  	LOG_ERROR           = 0x00000002,
>  	LOG_WARNING         = 0x00000004,
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index d6c599d5..13d5f6ca 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -247,7 +247,7 @@ fwts_log_field fwts_log_str_to_field(const char *text)
>  	for (i = 0; mappings[i].text != NULL; i++)
>  		if (strcmp(mappings[i].text, text) == 0)
>  			return mappings[i].field;
> -	return 0;
> +	return LOG_NO_FIELD;
>  }
>
>  /*
> @@ -286,12 +286,12 @@ void fwts_log_set_field_filter(const char *str)
>  			break;
>  		if (*token == '^' || *token == '~') {
>  			field = fwts_log_str_to_field(token+1);
> -			if (field)
> +			if (field != LOG_NO_FIELD)
>  				fwts_log_filter_unset_field(field);
>  		}
>  		else {
>  			field = fwts_log_str_to_field(token);
> -			if (field)
> +			if (field != LOG_NO_FIELD)
>  				fwts_log_filter_set_field(field);
>  		}
>  	}
>
Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu April 21, 2017, 3:01 a.m. UTC | #2
On 04/19/2017 06:08 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> Rather than implicitly using a non-enum fwts_log_field value zero
> to indicate an unfound field instead use a new explicitly defined
> LOG_NO_FIELD enum value to indicate this.  This cleans up a couple
> of static analysis warnings of the form:
>
> "The variable 'field' is of enum type. It is odd that it is used as a
> variable of a Boolean-type."
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  src/lib/include/fwts_log.h | 1 +
>  src/lib/src/fwts_log.c     | 6 +++---
>  2 files changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
> index 3c7dd4d3..028ba36c 100644
> --- a/src/lib/include/fwts_log.h
> +++ b/src/lib/include/fwts_log.h
> @@ -30,6 +30,7 @@
>  #define LOG_MAX_BUF_SIZE	(4096)		/* Max output per log line */
>
>  typedef enum {
> +	LOG_NO_FIELD        = 0x00000000,
>  	LOG_RESULT	    = 0x00000001,
>  	LOG_ERROR           = 0x00000002,
>  	LOG_WARNING         = 0x00000004,
> diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
> index d6c599d5..13d5f6ca 100644
> --- a/src/lib/src/fwts_log.c
> +++ b/src/lib/src/fwts_log.c
> @@ -247,7 +247,7 @@ fwts_log_field fwts_log_str_to_field(const char *text)
>  	for (i = 0; mappings[i].text != NULL; i++)
>  		if (strcmp(mappings[i].text, text) == 0)
>  			return mappings[i].field;
> -	return 0;
> +	return LOG_NO_FIELD;
>  }
>
>  /*
> @@ -286,12 +286,12 @@ void fwts_log_set_field_filter(const char *str)
>  			break;
>  		if (*token == '^' || *token == '~') {
>  			field = fwts_log_str_to_field(token+1);
> -			if (field)
> +			if (field != LOG_NO_FIELD)
>  				fwts_log_filter_unset_field(field);
>  		}
>  		else {
>  			field = fwts_log_str_to_field(token);
> -			if (field)
> +			if (field != LOG_NO_FIELD)
>  				fwts_log_filter_set_field(field);
>  		}
>  	}
>

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

Patch

diff --git a/src/lib/include/fwts_log.h b/src/lib/include/fwts_log.h
index 3c7dd4d3..028ba36c 100644
--- a/src/lib/include/fwts_log.h
+++ b/src/lib/include/fwts_log.h
@@ -30,6 +30,7 @@ 
 #define LOG_MAX_BUF_SIZE	(4096)		/* Max output per log line */
 
 typedef enum {
+	LOG_NO_FIELD        = 0x00000000,
 	LOG_RESULT	    = 0x00000001,
 	LOG_ERROR           = 0x00000002,
 	LOG_WARNING         = 0x00000004,
diff --git a/src/lib/src/fwts_log.c b/src/lib/src/fwts_log.c
index d6c599d5..13d5f6ca 100644
--- a/src/lib/src/fwts_log.c
+++ b/src/lib/src/fwts_log.c
@@ -247,7 +247,7 @@  fwts_log_field fwts_log_str_to_field(const char *text)
 	for (i = 0; mappings[i].text != NULL; i++)
 		if (strcmp(mappings[i].text, text) == 0)
 			return mappings[i].field;
-	return 0;
+	return LOG_NO_FIELD;
 }
 
 /*
@@ -286,12 +286,12 @@  void fwts_log_set_field_filter(const char *str)
 			break;
 		if (*token == '^' || *token == '~') {
 			field = fwts_log_str_to_field(token+1);
-			if (field)
+			if (field != LOG_NO_FIELD)
 				fwts_log_filter_unset_field(field);
 		}
 		else {
 			field = fwts_log_str_to_field(token);
-			if (field)
+			if (field != LOG_NO_FIELD)
 				fwts_log_filter_set_field(field);
 		}
 	}