threads / patch / 41517

patchChange type of signed int flags to unsigned

Subject: [PATCH] Change type of signed int flags to unsigned

## tl;dr

2 messages between Feb 25, 2016 and Feb 28, 2016. Diffs are folded; open one to read it.

replies: 1people: 2as markdown or json

Saurav Sachidanand· Feb 25, 2016, 13:24 UTC · lore

“pattern” and “exclude” are two structs defined in attr.c and dir.h respectively. Each contains a field named “flags” of type int, that takes on values from the set of positive integers {1, 4, 8, 16} enumerated through the macro EXC_FLAG_*.

That the most significant bit (used to store the sign) of these two fields is not used in any special way, is observed from the fact that, the flags fields (accessed within attr.c, dir.c, and builtin/check-ignore.c) is either checked for it's value using the & operator (e.g.: flags & EXC_FLAG_NODIR), or assigned a value of 0 first and then assigned any one of {1, 4, 8, 16} using the | operator (e.g.: flags |= EXC_FLAG_NODIR). Hence, change the type of flags to unsigned in both structs.

Furthermore, flags is passed by reference to the function parse_exclude_pattern defined in dir.c, which accepts an “int *” type for the flags argument. To avoid converting between pointers to different types, change type of the flags argument to “unsigned *”.

While we’re at it, document the flags field of exclude to explicitly state the values it’s supposed to take on.

Signed-off-by: Saurav Sachidanand <sauravsachidanand@gmail.com>
---
 attr.c | 2 +-
 dir.c  | 4 ++--
 dir.h  | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)
Show changes to 3 files +5 −6

attr.c, dir.c, dir.h

diff --git a/attr.c b/attr.c
index 086c08d..679e13c 100644
--- a/attr.c
+++ b/attr.c
@@ -124,7 +124,7 @@ struct pattern {
 	const char *pattern;
 	int patternlen;
 	int nowildcardlen;
-	int flags;		/* EXC_FLAG_* */
+	unsigned flags;		/* EXC_FLAG_* */
 };

 /*
diff --git a/dir.c b/dir.c
index 552af23..d36fda7 100644
--- a/dir.c
+++ b/dir.c
@@ -459,7 +459,7 @@ int no_wildcard(const char *string)

 void parse_exclude_pattern(const char **pattern,
 			   int *patternlen,
-			   int *flags,
+			   unsigned *flags,
 			   int *nowildcardlen)
 {
 	const char *p = *pattern;
@@ -500,7 +500,7 @@ void add_exclude(const char *string, const char *base,
 {
 	struct exclude *x;
 	int patternlen;
-	int flags;
+	unsigned flags;
 	int nowildcardlen;

 	parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
diff --git a/dir.h b/dir.h
index 3ec3fb0..e34df5e 100644
--- a/dir.h
+++ b/dir.h
@@ -28,7 +28,7 @@ struct exclude {
 	int nowildcardlen;
 	const char *base;
 	int baselen;
-	int flags;
+	unsigned flags;		/* EXC_FLAG_* */

 	/*
 	 * Counting starts from 1 for line numbers in ignore files,
@@ -244,7 +244,7 @@ extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
 extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
 					  struct exclude_list *el, int check_index);
 extern void add_excludes_from_file(struct dir_struct *, const char *fname);
-extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
+extern void parse_exclude_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
 extern void add_exclude(const char *string, const char *base,
 			int baselen, struct exclude_list *el, int srcpos);
 extern void clear_exclude_list(struct exclude_list *el);
--
2.7.1.339.g0233b80

This patch is for the suggested microproject for GSoC 2016 titled
"Use unsigned integral type for collection of bits."
Eric Sunshine· Feb 28, 2016, 07:59 UTC · re: Saurav Sachidanand · lore

Re: [PATCH] Change type of signed int flags to unsigned

On Thu, Feb 25, 2016 at 8:24 AM, Saurav Sachidanand <sauravsachidanand@gmail.com> wrote:

Show 18 quoted lines
> “pattern” and “exclude” are two structs defined in attr.c and dir.h
> respectively. Each contains a field named “flags” of type int, that
> takes on values from the set of positive integers {1, 4, 8, 16}
> enumerated through the macro EXC_FLAG_*.
>
> That the most significant bit (used to store the sign) of these two
> fields is not used in any special way, is observed from the fact
> that, the flags fields (accessed within attr.c, dir.c, and
> builtin/check-ignore.c) is either checked for it's value using the &
> operator (e.g.: flags & EXC_FLAG_NODIR), or assigned a value of 0
> first and then assigned any one of {1, 4, 8, 16} using the | operator
> (e.g.: flags |= EXC_FLAG_NODIR). Hence, change the type of flags
> to unsigned in both structs.
>
> Furthermore, flags is passed by reference to the function
> parse_exclude_pattern defined in dir.c, which accepts an “int *” type
> for the flags argument. To avoid converting between pointers to
> different types, change type of the flags argument to “unsigned *”.

If you follow Duy's suggestion[1] of checking for additional sign-conversion warnings and fixing the additional instances, then there will be a bit more fallout than this one function. In that case, you might consider changing this paragraph to be a bit more generic; for instance, perhaps something like this:

    These "flags" fields are passed to several functions, so convert
    the functions to accept 'unsigned' rather than 'int', as well.
> While we’re at it, document the flags field of exclude to explicitly
> state the values it’s supposed to take on.
Good.
> Signed-off-by: Saurav Sachidanand <sauravsachidanand@gmail.com>
> ---

Right here below the "---" line is where you'd write commentary about the patch. In this case, you should mention that this is a GSoC microproject.

As an aid to reviewers, also provide a link to the previous version (like this [2]), and explain what changed since that attempt. In this case, you refined the commit message and used bare "unsigned" in the patch rather than "unsigned int".

The patch itself looks reasonable (though it would look better if additional fixes were made as suggested by Duy[1]).

[1]: http://thread.gmane.org/gmane.comp.version-control.git/286821/focus=286900 [2]: http://thread.gmane.org/gmane.comp.version-control.git/286821

Show 64 quoted lines
>  attr.c | 2 +-
>  dir.c  | 4 ++--
>  dir.h  | 4 ++--
>  3 files changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/attr.c b/attr.c
> index 086c08d..679e13c 100644
> --- a/attr.c
> +++ b/attr.c
> @@ -124,7 +124,7 @@ struct pattern {
>         const char *pattern;
>         int patternlen;
>         int nowildcardlen;
> -       int flags;              /* EXC_FLAG_* */
> +       unsigned flags;         /* EXC_FLAG_* */
>  };
>
>  /*
> diff --git a/dir.c b/dir.c
> index 552af23..d36fda7 100644
> --- a/dir.c
> +++ b/dir.c
> @@ -459,7 +459,7 @@ int no_wildcard(const char *string)
>
>  void parse_exclude_pattern(const char **pattern,
>                            int *patternlen,
> -                          int *flags,
> +                          unsigned *flags,
>                            int *nowildcardlen)
>  {
>         const char *p = *pattern;
> @@ -500,7 +500,7 @@ void add_exclude(const char *string, const char *base,
>  {
>         struct exclude *x;
>         int patternlen;
> -       int flags;
> +       unsigned flags;
>         int nowildcardlen;
>
>         parse_exclude_pattern(&string, &patternlen, &flags, &nowildcardlen);
> diff --git a/dir.h b/dir.h
> index 3ec3fb0..e34df5e 100644
> --- a/dir.h
> +++ b/dir.h
> @@ -28,7 +28,7 @@ struct exclude {
>         int nowildcardlen;
>         const char *base;
>         int baselen;
> -       int flags;
> +       unsigned flags;         /* EXC_FLAG_* */
>
>         /*
>          * Counting starts from 1 for line numbers in ignore files,
> @@ -244,7 +244,7 @@ extern struct exclude_list *add_exclude_list(struct dir_struct *dir,
>  extern int add_excludes_from_file_to_list(const char *fname, const char *base, int baselen,
>                                           struct exclude_list *el, int check_index);
>  extern void add_excludes_from_file(struct dir_struct *, const char *fname);
> -extern void parse_exclude_pattern(const char **string, int *patternlen, int *flags, int *nowildcardlen);
> +extern void parse_exclude_pattern(const char **string, int *patternlen, unsigned *flags, int *nowildcardlen);
>  extern void add_exclude(const char *string, const char *base,
>                         int baselen, struct exclude_list *el, int srcpos);
>  extern void clear_exclude_list(struct exclude_list *el);
> --
> 2.7.1.339.g0233b80

← back to recent threads