From: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
To: linux-sparse@vger.kernel.org
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
Christopher Li <sparse@chrisli.org>,
Dibyendu Majumdar <mobile@majumdar.org.uk>,
Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
Subject: [RFC PATCH 12/14] cast: add support for -Wpointer-to-int-cast
Date: Thu, 17 Aug 2017 06:05:27 +0200 [thread overview]
Message-ID: <20170817040529.7289-13-luc.vanoostenryck@gmail.com> (raw)
In-Reply-To: <20170817040529.7289-1-luc.vanoostenryck@gmail.com>
It's relatively common to cast a pointer to an unsigned long,
for example to make some bit operations.
It's much less sensicla to cast a pointer to an integer smaller
(or bigger) than a pointer is.
So allow to emit a diagnostic for this, under the control of a
new warning flag: -Wpointer-to-cast.
Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
---
lib.c | 2 ++
lib.h | 1 +
linearize.c | 5 +++++
validation/cast-kinds-check.c | 2 +-
validation/cast-kinds.c | 2 ++
5 files changed, 11 insertions(+), 1 deletion(-)
diff --git a/lib.c b/lib.c
index 1c67e1912..7bfe9eb4e 100644
--- a/lib.c
+++ b/lib.c
@@ -241,6 +241,7 @@ int Woverride_init = 1;
int Woverride_init_all = 0;
int Woverride_init_whole_range = 0;
int Wparen_string = 0;
+int Wpointer_to_int_cast = 1;
int Wptr_subtraction_blows = 0;
int Wreturn_void = 0;
int Wshadow = 0;
@@ -532,6 +533,7 @@ static const struct warning {
{ "override-init", &Woverride_init },
{ "override-init-all", &Woverride_init_all },
{ "paren-string", &Wparen_string },
+ { "pointer-to-int-cast", &Wpointer_to_int_cast },
{ "ptr-subtraction-blows", &Wptr_subtraction_blows },
{ "return-void", &Wreturn_void },
{ "shadow", &Wshadow },
diff --git a/lib.h b/lib.h
index 010317970..e588bb8c5 100644
--- a/lib.h
+++ b/lib.h
@@ -134,6 +134,7 @@ extern int Woverride_init;
extern int Woverride_init_all;
extern int Woverride_init_whole_range;
extern int Wparen_string;
+extern int Wpointer_to_int_cast;
extern int Wptr_subtraction_blows;
extern int Wreturn_void;
extern int Wshadow;
diff --git a/linearize.c b/linearize.c
index a1b647864..cae402ad3 100644
--- a/linearize.c
+++ b/linearize.c
@@ -1246,6 +1246,11 @@ static pseudo_t cast_pseudo(struct entrypoint *ep, pseudo_t src, struct symbol *
src = cast_pseudo(ep, src, from, size_t_ctype);
from = size_t_ctype;
break;
+ case OP_PTRTU:
+ if (from->bit_size == to->bit_size)
+ break;
+ if (Wpointer_to_int_cast)
+ warning(to->pos, "non size-preserving pointer to integer cast");
default:
break;
}
diff --git a/validation/cast-kinds-check.c b/validation/cast-kinds-check.c
index 365fe6e40..0eb94d047 100644
--- a/validation/cast-kinds-check.c
+++ b/validation/cast-kinds-check.c
@@ -2,7 +2,7 @@
/*
* check-name: cast-kinds check
- * check-command: sparse -m64 -v $file
+ * check-command: sparse -m64 -v -Wno-pointer-to-int-cast $file
*
* check-error-start
cast-kinds.c:5:45: warning: cast drops bits
diff --git a/validation/cast-kinds.c b/validation/cast-kinds.c
index f60013e68..747a181ce 100644
--- a/validation/cast-kinds.c
+++ b/validation/cast-kinds.c
@@ -58,6 +58,8 @@ static double double_2_double(double a) { return a; }
* check-command: test-linearize -m64 $file
*
* check-error-start
+cast-kinds.c:8:41: warning: non size-preserving pointer to integer cast
+cast-kinds.c:15:43: warning: non size-preserving pointer to integer cast
cast-kinds.c:37:42: warning: non size-preserving integer to pointer cast
cast-kinds.c:38:44: warning: non size-preserving integer to pointer cast
* check-error-end
--
2.14.0
next prev parent reply other threads:[~2017-08-17 4:06 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-08-17 4:05 [RFC PATCH 00/14] rework of cast operations Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 01/14] add documentation for IR instructions Luc Van Oostenryck
2017-08-21 12:18 ` Christopher Li
2017-08-17 4:05 ` [RFC PATCH 02/14] cast: add tests for warnings issued by sparse -v Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 03/14] cast: prepare finer grained cast instructions Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 04/14] cast: specialize FPCAST into [USF]CVTF Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 05/14] cast: handle NO-OP casts Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 06/14] cast: specialize floats to integer conversion Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 07/14] cast: specialize casts from unsigned to pointers Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 08/14] cast: make [u]intptr_ctype alias of [s]size_t_ctype Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 09/14] cast: make pointer casts always size preserving Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 10/14] cast: temporary simplify handling cast to/from void* Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 11/14] cast: specialize cast from pointers Luc Van Oostenryck
2017-08-17 4:05 ` Luc Van Oostenryck [this message]
2017-08-17 4:05 ` [RFC PATCH 13/14] cast: make casts from pointer always size preserving Luc Van Oostenryck
2017-08-17 4:05 ` [RFC PATCH 14/14] cast: specialize integer casts Luc Van Oostenryck
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170817040529.7289-13-luc.vanoostenryck@gmail.com \
--to=luc.vanoostenryck@gmail.com \
--cc=linux-sparse@vger.kernel.org \
--cc=mobile@majumdar.org.uk \
--cc=sparse@chrisli.org \
--cc=torvalds@linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).