[PATCH 17/17] big-shift: add -Wshift-count-{negative,overflow}

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The warnings about negative or over-sized shift count are
enabled by default.

Add the options to disabled them if whished so.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx>
---
 expand.c   |  5 +++++
 lib.c      |  4 ++++
 lib.h      |  2 ++
 simplify.c |  3 ++-
 sparse.1   | 12 ++++++++++++
 5 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/expand.c b/expand.c
index 5dace8e30..45e6f95e1 100644
--- a/expand.c
+++ b/expand.c
@@ -164,6 +164,8 @@ static void check_shift_count(struct expression *expr, struct expression *right)
 	long long count = get_longlong(right);
 
 	if (count < 0) {
+		if (!Wshift_count_negative)
+			return;
 		warning(expr->pos, "shift count is negative (%lld)", count);
 		return;
 	}
@@ -171,6 +173,9 @@ static void check_shift_count(struct expression *expr, struct expression *right)
 		return;
 	if (ctype->type == SYM_NODE)
 		ctype = ctype->ctype.base_type;
+
+	if (!Wshift_count_overflow)
+		return;
 	warning(expr->pos, "shift too big (%llu) for type %s", count, show_typename(ctype));
 }
 
diff --git a/lib.c b/lib.c
index 308f8f699..2d87b567d 100644
--- a/lib.c
+++ b/lib.c
@@ -274,6 +274,8 @@ int Wpointer_to_int_cast = 1;
 int Wptr_subtraction_blows = 0;
 int Wreturn_void = 0;
 int Wshadow = 0;
+int Wshift_count_negative = 1;
+int Wshift_count_overflow = 1;
 int Wsizeof_bool = 0;
 int Wtautological_compare = 0;
 int Wtransparent_union = 0;
@@ -701,6 +703,8 @@ static const struct flag warnings[] = {
 	{ "ptr-subtraction-blows", &Wptr_subtraction_blows },
 	{ "return-void", &Wreturn_void },
 	{ "shadow", &Wshadow },
+	{ "shift-count-negative", &Wshift_count_negative },
+	{ "shift-count-overflow", &Wshift_count_overflow },
 	{ "sizeof-bool", &Wsizeof_bool },
 	{ "pointer-arith", &Wpointer_arith },
 	{ "sparse-error", &Wsparse_error },
diff --git a/lib.h b/lib.h
index b0453bb6e..7362111cd 100644
--- a/lib.h
+++ b/lib.h
@@ -163,6 +163,8 @@ extern int Wpointer_to_int_cast;
 extern int Wptr_subtraction_blows;
 extern int Wreturn_void;
 extern int Wshadow;
+extern int Wshift_count_negative;
+extern int Wshift_count_overflow;
 extern int Wsizeof_bool;
 extern int Wtautological_compare;
 extern int Wtransparent_union;
diff --git a/simplify.c b/simplify.c
index 19ff47a32..3a5ae9331 100644
--- a/simplify.c
+++ b/simplify.c
@@ -549,7 +549,8 @@ static int simplify_shift(struct instruction *insn, pseudo_t pseudo, long long v
 
 	size = insn->size;
 	if (value >= size && !insn->tainted) {
-		warning(insn->pos, "shift by bigger than operand's width");
+		if (Wshift_count_overflow)
+			warning(insn->pos, "shift by bigger than operand's width");
 		insn->tainted = 1;
 	}
 	switch (insn->opcode) {
diff --git a/sparse.1 b/sparse.1
index 806fb0cf0..8a14a6be3 100644
--- a/sparse.1
+++ b/sparse.1
@@ -339,6 +339,18 @@ Such declarations can lead to error-prone code.
 Sparse does not issue these warnings by default.
 .
 .TP
+.B \-Wshift-count-negative
+Warn if a shift count is negative.
+
+Sparse issues these warnings by default.
+.
+.TP
+.B \-Wshift-count-overflow
+Warn if a shift count is bigger than the operand's width.
+
+Sparse issues these warnings by default.
+.
+.TP
 .B \-Wsizeof-bool
 Warn when checking the sizeof a _Bool.
 
-- 
2.18.0

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux