FS: Fixing return type of unsigned_offsets
Subsystems (2)
PATCH 1/1 FS: Fixing return type of unsigned_offsets
Fixing Sparse warning. It should return bool, instead it returns int.
--- fs/read_write.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
fs/read_write.c +2 -2
@@ -33,9 +33,9 @@ const struct file_operations generic_ro_fops = {EXPORT_SYMBOL (generic_ro_fops); -static inline int unsigned_offsets (struct file *file) +static inline bool unsigned_offsets (struct file *file) { -return file->f_mode &FMODE_UNSIGNED_OFFSET ; +return !!(file->f_mode &FMODE_UNSIGNED_OFFSET ); }/**
Signature · 1 line
2.7.4
Quoted 2 lines · Pushkar Jambhlekar
Fixing Sparse warning. It should return bool, instead it returns int.
[]
fs/read_write.c
Quoted 1 line
fs/read_write.c +0
-0
[]
fs/read_write.c const struct file_operations generic_ro_fops = { · line 38
Quoted 9 lines
fs/read_write.c +2
-2@@ -33,9 +33,9 @@ const struct file_operations generic_ro_fops = {EXPORT_SYMBOL (generic_ro_fops); -static inline int unsigned_offsets (struct file *file) +static inline bool unsigned_offsets (struct file *file) { -return file->f_mode &FMODE_UNSIGNED_OFFSET ; +return !!(file->f_mode &FMODE_UNSIGNED_OFFSET );
trivia: the !! isn't necessary as by definition
all non-zero assigns to bool are converted to 1.
Should I change my implementation, i.e. remove '!!'?
Quoted 19 lines · Joe Perches
Quoted 2 lines · Pushkar Jambhlekar
Fixing Sparse warning. It should return bool, instead it returns int.
[]
fs/read_write.c
Quoted 1 line
fs/read_write.c +0
-0[]
fs/read_write.c const struct file_operations generic_ro_fops = { · line 38
Quoted 9 lines
fs/read_write.c +2
-2@@ -33,9 +33,9 @@ const struct file_operations generic_ro_fops = {EXPORT_SYMBOL (generic_ro_fops); -static inline int unsigned_offsets (struct file *file) +static inline bool unsigned_offsets (struct file *file) { -return file->f_mode &FMODE_UNSIGNED_OFFSET ; +return !!(file->f_mode &FMODE_UNSIGNED_OFFSET );trivia: the !! isn't necessary as by definition
all non-zero assigns to bool are converted to 1.
Signature · 1 line
Jambhlekar Pushkar Arun
Quoted 1 line · Pushkar Jambhlekar
Should I change my implementation, i.e. remove '!!'?
That'd be up to Al.
At least one implementation using similar bit comparisons in fs/*.c does not use !!
fs/locks.c:static bool lease_breaking(struct file_lock *fl)fs/locks.c-{
fs/locks.c-�����return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING) fs/locks.c-}
I didn't look very hard.
If I remove '!!', sparse flags warning:
fs/read_write.c:38:29: warning: incorrect type in return expression (different base types)fs/read_write.c:38:29: expected boolfs/read_write.c:38:29: got restricted fmode_t
It means explicit conversion is needed.
Quoted 14 lines · Joe Perches
Quoted 1 line · Pushkar Jambhlekar
Should I change my implementation, i.e. remove '!!'?
That'd be up to Al.
At least one implementation using similar bit comparisons in
fs/*.cdoes not use !!fs/locks.c:static bool
lease_breaking(struct file_lock *fl)fs/locks.c-{fs/locks.c-returnfl->fl_flags& (FL_UNLOCK_PENDING|FL_DOWNGRADE_PENDING)fs/locks.c-}I didn't look very hard.
Signature · 1 line
Jambhlekar Pushkar Arun
Al Viro
maintainerQuoted 8 lines · Pushkar Jambhlekar
If I remove '!!', sparse flags warning:
fs/read_write.c:38:29: warning: incorrect type in return expression (different base types)fs/read_write.c:38:29: expected boolfs/read_write.c:38:29: got restrictedfmode_tIt means explicit conversion is needed.
FVO"needed" equal to "needed to make sparse STFU"? If anything, that's sparse being wrong - evaluate.c:check_assignment_type() should do
if (t == &ctype_bool) {
if (is_fouled_type(s))
warning((*rp)->pos, "%s degrades to integer",
show_typename(s->ctype.base_type));
goto Cast;
}
right after
} else if (!(sclass & TYPE_RESTRICT))
goto Cast;PATCH 1/1 avoid useless warning for 'bool <- restricted type' conversion
Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning.
Fix this by catching such 'bool <- restricted type' conversion and avoid such warnings.
--- evaluate.c | 6 ++++++ validation/bool-cast-restricted.c | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 validation/bool-cast-restricted.c
evaluate.c +6 -0
@@ -1345,6 +1345,12 @@ static int check_assignment_types(struct symbol *target, struct expression **rp,return 1 ; }else if (!(sclass &TYPE_RESTRICT )) goto Cast; +if (t == &bool_ctype) { +if (is_fouled_type (s)) +warning ((*rp)->pos,"%s degrades to integer" , +show_typename (s->ctype.base_type)); + goto Cast; + } *typediff ="different base types" ;return 0 ; }
validation/bool-cast-restricted.c new file +25 -0
@@ -0,0 +1,25 @@ +typedef unsigned int __attribute__((bitwise)) large_t; +#define LBIT ((__attribute__((force)) large_t) 1) + +_Bool lfoo (large_t x) {return x; } +_Bool lbar (large_t x) {return ~x; } +_Bool lbaz (large_t x) {return !x; } +_Bool lqux (large_t x) {return x &LBIT ; } + + +typedef unsigned short __attribute__((bitwise)) small_t; +#define SBIT ((__attribute__((force)) small_t) 1) + +_Bool sfoo (small_t x) {return x; } +_Bool sbar (small_t x) {return ~x; } +_Bool sbaz (small_t x) {return !x; } +_Bool squx (small_t x) {return x &SBIT ; } + +/* +* check-name: bool-cast-restricted.c +* check-command: sparse -Wno-decl $file +* +* check-error-start +bool-cast-restricted.c:14:32: warning: restricted small_t degrades to integer +* check-error-end +*/
Signature · 1 line
2.12.2
Christopher Li
maintainerRe: [PATCH] avoid useless warning for 'bool <- restricted type' conversion
Quoted 11 lines · Luc Van Oostenryck
Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning.
Fix this by catching such 'bool <- restricted type' conversion and avoid such warnings.
The change seems fine. However, have you run the test-suite with this change? It seems cause some test do not pass. I assume it is cause by different error message it will output.
Chris
Re: [PATCH] avoid useless warning for 'bool <- restricted type' conversion
Quoted 11 lines · Christopher Li
Quoted 9 lines · Luc Van Oostenryck
Conversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning.
Quoted 6 lines
Quoted 2 lines
Fix this by catching such 'bool <- restricted type' conversion and avoid such warnings.
The change seems fine. However, have you run the test-suite with this change? It seems cause some test do not pass. I assume it is cause by different error message it will output.
Oh my, sorry.
I *have* run the test-suite but then I've forgotten to commit the change needed for two test cases.
Thanks for noticing this.
-- Luc
Quoted 11 lines · Al Viro
FVO"needed" equal to "needed to make sparse STFU"? If anything, that's sparse being wrong - evaluate.c:check_assignment_type() should do
if (t == &ctype_bool) { if (is_fouled_type(s)) warning((*rp)->pos, "%s degrades to integer", show_typename(s->ctype.base_type)); goto Cast; } right after } else if (!(sclass & TYPE_RESTRICT)) goto Cast;
What about an explicit cast of restricted types to bool? I think we would want the equivalent of this patch for those too.
-- Luc
PATCH 1/1 avoid useless warning for 'bool <- restricted type' conversion
In reply to Christopher LiConversion to bool is special in C since this conversion is essentially the result of the comparison with zero. As such, some operations which are normally unsafe to do with restricted types, like casting to an unrestricted type, are in fact safe to do when converting to bool and issuing a warning in those case is useless, confusing and causes people to add useless casts in the code in order to shut up the warning.
Fix this by catching such 'bool <- restricted type' conversion and avoid such warnings.
--- evaluate.c | 6 ++++++ validation/bool-cast-bad.c | 3 --- validation/bool-cast-implicit.c | 3 --- validation/bool-cast-restricted.c | 25 +++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 validation/bool-cast-restricted.c
evaluate.c +6 -0
@@ -1345,6 +1345,12 @@ static int check_assignment_types(struct symbol *target, struct expression **rp,return 1 ; }else if (!(sclass &TYPE_RESTRICT )) goto Cast; +if (t == &bool_ctype) { +if (is_fouled_type (s)) +warning ((*rp)->pos,"%s degrades to integer" , +show_typename (s->ctype.base_type)); + goto Cast; + } *typediff ="different base types" ;return 0 ; }
validation/bool-cast-bad.c +0 -3
@@ -15,9 +15,6 @@ static _Bool fstse(struct s a) { return (_Bool)a; }* check-command: sparse $file * * check-error-start -bool-cast-bad.c:8:41: warning: incorrect type in return expression (different base types) -bool-cast-bad.c:8:41: expected bool -bool-cast-bad.c:8:41: got restricted le16 [usertype] a bool-cast-bad.c:9:42: warning: cast from restricted le16 bool-cast-bad.c:10:41: warning: incorrect type in return expression (different base types) bool-cast-bad.c:10:41: expected bool
validation/bool-cast-implicit.c +0 -3
@@ -21,8 +21,5 @@ static _Bool fres(le16 a) { return a; }* check-output-excludes: cast\\. * * check-error-start -bool-cast-implicit.c:15:36: warning: incorrect type in return expression (different base types) -bool-cast-implicit.c:15:36: expected bool -bool-cast-implicit.c:15:36: got restricted le16 [usertype] a * check-error-end */
validation/bool-cast-restricted.c new file +25 -0
@@ -0,0 +1,25 @@ +typedef unsigned int __attribute__((bitwise)) large_t; +#define LBIT ((__attribute__((force)) large_t) 1) + +_Bool lfoo (large_t x) {return x; } +_Bool lbar (large_t x) {return ~x; } +_Bool lbaz (large_t x) {return !x; } +_Bool lqux (large_t x) {return x &LBIT ; } + + +typedef unsigned short __attribute__((bitwise)) small_t; +#define SBIT ((__attribute__((force)) small_t) 1) + +_Bool sfoo (small_t x) {return x; } +_Bool sbar (small_t x) {return ~x; } +_Bool sbaz (small_t x) {return !x; } +_Bool squx (small_t x) {return x &SBIT ; } + +/* +* check-name: bool-cast-restricted.c +* check-command: sparse -Wno-decl $file +* +* check-error-start +bool-cast-restricted.c:14:32: warning: restricted small_t degrades to integer +* check-error-end +*/
Signature · 1 line
2.12.2