diff mbox

avoid useless warning for 'bool <- restricted type' conversion

Message ID 20170511162127.15692-1-luc.vanoostenryck@gmail.com (mailing list archive)
State Superseded, archived
Headers show

Commit Message

Luc Van Oostenryck May 11, 2017, 4:21 p.m. UTC
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.

CC: Al Viro <viro@zeniv.linux.org.uk>
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-restricted.c | 25 +++++++++++++++++++++++++
 2 files changed, 31 insertions(+)
 create mode 100644 validation/bool-cast-restricted.c

Comments

Christopher Li May 11, 2017, 7:41 p.m. UTC | #1
On Thu, May 11, 2017 at 9:21 AM, Luc Van Oostenryck
<luc.vanoostenryck@gmail.com> wrote:
> 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
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Luc Van Oostenryck May 11, 2017, 7:48 p.m. UTC | #2
On Thu, May 11, 2017 at 9:41 PM, Christopher Li <sparse@chrisli.org> wrote:
> On Thu, May 11, 2017 at 9:21 AM, Luc Van Oostenryck
> <luc.vanoostenryck@gmail.com> wrote:
>> 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.

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
--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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-restricted.c b/validation/bool-cast-restricted.c
new file mode 100644
index 000000000..f6776b050
--- /dev/null
+++ b/validation/bool-cast-restricted.c
@@ -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
+ */