linux-sparse.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] avoid warnings for 'bool <- restricted' casts
@ 2017-05-19  2:47 Luc Van Oostenryck
  2017-05-19  2:47 ` [PATCH v3 1/2] avoid warning on implicit " Luc Van Oostenryck
  2017-05-19  2:47 ` [PATCH v3 2/2] avoid warning on explicit " Luc Van Oostenryck
  0 siblings, 2 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2017-05-19  2:47 UTC (permalink / raw)
  To: linux-sparse; +Cc: Al Viro, Luc Van Oostenryck

The goal of this series is to stop to emit a warning
when a restricted type is casted to the boolean type
as such conversion is equivalent to a comparision
againts zero for which the 'restrictness' doesn't matter.


Change since v2:
- add tests for assignment to bool type.
- add patch & tests for explicit casts

Changes since v1:
- fix forgotten adjustment of 2 existing test cases.


Luc Van Oostenryck (2):
  avoid warning on implicit 'bool <- restricted' casts
  avoid warning on explicit 'bool <- restricted' casts

 evaluate.c                        | 19 ++++++++++++++---
 validation/bool-cast-bad.c        |  4 ----
 validation/bool-cast-explicit.c   |  4 ----
 validation/bool-cast-implicit.c   |  3 ---
 validation/bool-cast-restricted.c | 43 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 59 insertions(+), 14 deletions(-)
 create mode 100644 validation/bool-cast-restricted.c

-- 
2.13.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v3 1/2] avoid warning on implicit 'bool <- restricted' casts
  2017-05-19  2:47 [PATCH v3 0/2] avoid warnings for 'bool <- restricted' casts Luc Van Oostenryck
@ 2017-05-19  2:47 ` Luc Van Oostenryck
  2017-05-19  2:47 ` [PATCH v3 2/2] avoid warning on explicit " Luc Van Oostenryck
  1 sibling, 0 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2017-05-19  2:47 UTC (permalink / raw)
  To: linux-sparse; +Cc: Al Viro, 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 implicit 'bool <- restricted type' casts
and not emit a warning like for others casts of restricted
type to non-restrictes ones.

Originally-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                        |  6 ++++++
 validation/bool-cast-bad.c        |  3 ---
 validation/bool-cast-implicit.c   |  3 ---
 validation/bool-cast-restricted.c | 34 ++++++++++++++++++++++++++++++++++
 4 files changed, 40 insertions(+), 6 deletions(-)
 create mode 100644 validation/bool-cast-restricted.c

diff --git a/evaluate.c b/evaluate.c
index 976857915..3dc26fc09 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -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;
 	}
diff --git a/validation/bool-cast-bad.c b/validation/bool-cast-bad.c
index b7e7c058d..a0b091e1c 100644
--- a/validation/bool-cast-bad.c
+++ b/validation/bool-cast-bad.c
@@ -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
diff --git a/validation/bool-cast-implicit.c b/validation/bool-cast-implicit.c
index ee8b705b9..9d89443b1 100644
--- a/validation/bool-cast-implicit.c
+++ b/validation/bool-cast-implicit.c
@@ -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
  */
diff --git a/validation/bool-cast-restricted.c b/validation/bool-cast-restricted.c
new file mode 100644
index 000000000..0aa9f35b4
--- /dev/null
+++ b/validation/bool-cast-restricted.c
@@ -0,0 +1,34 @@
+typedef unsigned   int __attribute__((bitwise)) large_t;
+#define	LBIT	((__attribute__((force)) large_t) 1)
+
+_Bool lfoo(large_t x) { return x; }
+_Bool qfoo(large_t x) { _Bool r = x; return r; }
+_Bool lbar(large_t x) { return ~x; }
+_Bool qbar(large_t x) { _Bool r = ~x; return r; }
+_Bool lbaz(large_t x) { return !x; }
+_Bool qbaz(large_t x) { _Bool r = !x; return r; }
+_Bool lqux(large_t x) { return x & LBIT; }
+_Bool qqux(large_t x) { _Bool r = x & LBIT; return r; }
+
+
+typedef unsigned short __attribute__((bitwise)) small_t;
+#define	SBIT	((__attribute__((force)) small_t) 1)
+
+_Bool sfoo(small_t x) { return x; }
+_Bool tfoo(small_t x) { _Bool r = x; return r; }
+_Bool sbar(small_t x) { return ~x; }
+_Bool tbar(small_t x) { _Bool r = ~x; return r; }
+_Bool sbaz(small_t x) { return !x; }
+_Bool tbaz(small_t x) { _Bool r = !x; return r; }
+_Bool squx(small_t x) { return x & SBIT; }
+_Bool tqux(small_t x) { _Bool r = x & SBIT; return r; }
+
+/*
+ * check-name: bool-cast-restricted.c
+ * check-command: sparse -Wno-decl $file
+ *
+ * check-error-start
+bool-cast-restricted.c:19:32: warning: restricted small_t degrades to integer
+bool-cast-restricted.c:20:35: warning: restricted small_t degrades to integer
+ * check-error-end
+ */
-- 
2.13.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH v3 2/2] avoid warning on explicit 'bool <- restricted' casts
  2017-05-19  2:47 [PATCH v3 0/2] avoid warnings for 'bool <- restricted' casts Luc Van Oostenryck
  2017-05-19  2:47 ` [PATCH v3 1/2] avoid warning on implicit " Luc Van Oostenryck
@ 2017-05-19  2:47 ` Luc Van Oostenryck
  1 sibling, 0 replies; 3+ messages in thread
From: Luc Van Oostenryck @ 2017-05-19  2:47 UTC (permalink / raw)
  To: linux-sparse; +Cc: Al Viro, 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 explicit 'bool <- restricted type' casts
and not emit a warning like for others casts of restricted
type to non-restrictes ones.

Based-on-patch-by: Al Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
 evaluate.c                        | 13 ++++++++++---
 validation/bool-cast-bad.c        |  1 -
 validation/bool-cast-explicit.c   |  4 ----
 validation/bool-cast-restricted.c | 13 +++++++++++--
 4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/evaluate.c b/evaluate.c
index 3dc26fc09..6bcccde2a 100644
--- a/evaluate.c
+++ b/evaluate.c
@@ -2821,9 +2821,16 @@ static struct symbol *evaluate_cast(struct expression *expr)
 		if ((class1 & TYPE_RESTRICT) && restricted_value(target, t1))
 			warning(expr->pos, "cast to %s",
 				show_typename(t1));
-		if (class2 & TYPE_RESTRICT)
-			warning(expr->pos, "cast from %s",
-				show_typename(t2));
+		if (class2 & TYPE_RESTRICT) {
+			if (t1 == &bool_ctype) {
+				if (class2 & TYPE_FOULED)
+					warning(expr->pos, "%s degrades to integer",
+						show_typename(t2));
+			} else {
+				warning(expr->pos, "cast from %s",
+					show_typename(t2));
+			}
+		}
 	}
 
 	if (t1 == &ulong_ctype)
diff --git a/validation/bool-cast-bad.c b/validation/bool-cast-bad.c
index a0b091e1c..ae8b96018 100644
--- a/validation/bool-cast-bad.c
+++ b/validation/bool-cast-bad.c
@@ -15,7 +15,6 @@ static _Bool fstse(struct s a) { return (_Bool)a; }
  * check-command: sparse $file
  *
  * check-error-start
-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
 bool-cast-bad.c:10:41:    got struct s a
diff --git a/validation/bool-cast-explicit.c b/validation/bool-cast-explicit.c
index 4e3c2b7ce..dbb67cc42 100644
--- a/validation/bool-cast-explicit.c
+++ b/validation/bool-cast-explicit.c
@@ -19,8 +19,4 @@ static _Bool fres(le16 a) { return (_Bool)a; }
  * check-command: test-linearize -m64 $file
  * check-output-ignore
  * check-output-excludes: cast\\.
- *
- * check-error-start
-bool-cast-explicit.c:15:37: warning: cast from restricted le16
- * check-error-end
  */
diff --git a/validation/bool-cast-restricted.c b/validation/bool-cast-restricted.c
index 0aa9f35b4..9985d6e32 100644
--- a/validation/bool-cast-restricted.c
+++ b/validation/bool-cast-restricted.c
@@ -3,12 +3,16 @@ typedef unsigned   int __attribute__((bitwise)) large_t;
 
 _Bool lfoo(large_t x) { return x; }
 _Bool qfoo(large_t x) { _Bool r = x; return r; }
+_Bool xfoo(large_t x) { return (_Bool)x; }
 _Bool lbar(large_t x) { return ~x; }
 _Bool qbar(large_t x) { _Bool r = ~x; return r; }
+_Bool xbar(large_t x) { return (_Bool)~x; }
 _Bool lbaz(large_t x) { return !x; }
 _Bool qbaz(large_t x) { _Bool r = !x; return r; }
+_Bool xbaz(large_t x) { return (_Bool)!x; }
 _Bool lqux(large_t x) { return x & LBIT; }
 _Bool qqux(large_t x) { _Bool r = x & LBIT; return r; }
+_Bool xqux(large_t x) { return (_Bool)(x & LBIT); }
 
 
 typedef unsigned short __attribute__((bitwise)) small_t;
@@ -16,19 +20,24 @@ typedef unsigned short __attribute__((bitwise)) small_t;
 
 _Bool sfoo(small_t x) { return x; }
 _Bool tfoo(small_t x) { _Bool r = x; return r; }
+_Bool zfoo(small_t x) { return (_Bool)x; }
 _Bool sbar(small_t x) { return ~x; }
 _Bool tbar(small_t x) { _Bool r = ~x; return r; }
+_Bool zbar(small_t x) { return (_Bool)~x; }
 _Bool sbaz(small_t x) { return !x; }
 _Bool tbaz(small_t x) { _Bool r = !x; return r; }
+_Bool zbaz(small_t x) { return (_Bool)!x; }
 _Bool squx(small_t x) { return x & SBIT; }
 _Bool tqux(small_t x) { _Bool r = x & SBIT; return r; }
+_Bool zqux(small_t x) { return (_Bool)(x & SBIT); }
 
 /*
  * check-name: bool-cast-restricted.c
  * check-command: sparse -Wno-decl $file
  *
  * check-error-start
-bool-cast-restricted.c:19:32: warning: restricted small_t degrades to integer
-bool-cast-restricted.c:20:35: warning: restricted small_t degrades to integer
+bool-cast-restricted.c:24:32: warning: restricted small_t degrades to integer
+bool-cast-restricted.c:25:35: warning: restricted small_t degrades to integer
+bool-cast-restricted.c:26:33: warning: restricted small_t degrades to integer
  * check-error-end
  */
-- 
2.13.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-05-19  2:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-19  2:47 [PATCH v3 0/2] avoid warnings for 'bool <- restricted' casts Luc Van Oostenryck
2017-05-19  2:47 ` [PATCH v3 1/2] avoid warning on implicit " Luc Van Oostenryck
2017-05-19  2:47 ` [PATCH v3 2/2] avoid warning on explicit " Luc Van Oostenryck

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).