This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Warn when casting a pointer (constant) to an integer of different size.


The case "constant -> pointer -> integer" should have a warning when the
size of integer and pointer are not the same.

If the size of integer is greater than the pointer size it could produce
unwanted sign extension. This can help track down incorrect casts and
replace them with casts to uintptr_t.

And example could be:

	((unsigned long long)((void *)(0xdeadbeef)))

Should emit a warning, since the result could become 0xffffffffdeadbeef
instead of 0xdeadbeef. 

It should emit a warning in the more generic case when the precisions
are not equal, and that is what the patch does.  I've included fixes for
the regressions caused by the patch.

Tested on x86-pc-linux-gnu with no regressions.

Comments?

:ADDPATCH C front-end:

gcc/testsuite/

2005-12-10  Carlos O'Donell <carlos@codesourcery.com>

	* gcc.dg/cast-1.c: Add new warning.
	* gcc.dg/cast-2.c: Add new warning.
	* gcc.dg/cast-3.c: Add new warning.
	* gcc.dg/format/cast-1.c: Add new warning.

gcc/

2005-12-10  Carlos O'Donell <carlos@codesourcery.com>

	* gcc/c-typeck.c (): Always warn when casting from a pointer to
	an integer of different size, even if the node was constant.

Index: gcc/testsuite/gcc.dg/cast-2.c
===================================================================
--- gcc/testsuite/gcc.dg/cast-2.c	(revision 108361)
+++ gcc/testsuite/gcc.dg/cast-2.c	(working copy)
@@ -37,5 +37,5 @@
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
Index: gcc/testsuite/gcc.dg/cast-1.c
===================================================================
--- gcc/testsuite/gcc.dg/cast-1.c	(revision 108361)
+++ gcc/testsuite/gcc.dg/cast-1.c	(working copy)
@@ -37,5 +37,5 @@
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
Index: gcc/testsuite/gcc.dg/cast-3.c
===================================================================
--- gcc/testsuite/gcc.dg/cast-3.c	(revision 108361)
+++ gcc/testsuite/gcc.dg/cast-3.c	(working copy)
@@ -37,5 +37,5 @@
   (void *) c; /* { dg-warning "warning: cast to pointer from integer of different size" } */
   (void *) (char) 1;
   (char) p; /* { dg-warning "warning: cast from pointer to integer of different size" } */
-  (char) (void *) 1;
+  (char) (void *) 1; /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
Index: gcc/testsuite/gcc.dg/format/cast-1.c
===================================================================
--- gcc/testsuite/gcc.dg/format/cast-1.c	(revision 108361)
+++ gcc/testsuite/gcc.dg/format/cast-1.c	(working copy)
@@ -12,5 +12,5 @@
 {
   printf("%s", x); /* { dg-warning "format" } */
   printf((char *)(size_t)"%s", x); /* { dg-warning "format" } */
-  printf((char *)(char)"%s", x);
+  printf((char *)(char)"%s", x); /* { dg-warning "warning: cast from pointer to integer of different size" } */
 }
Index: gcc/c-typeck.c
===================================================================
--- gcc/c-typeck.c	(revision 108361)
+++ gcc/c-typeck.c	(working copy)
@@ -3464,8 +3464,9 @@
 
       if (TREE_CODE (type) == INTEGER_TYPE
 	  && TREE_CODE (otype) == POINTER_TYPE
-	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype)
-	  && !TREE_CONSTANT (value))
+	  && TYPE_PRECISION (type) != TYPE_PRECISION (otype))
+      /* Always Warn about converting constants. Constant to larger integer
+         may cause unwanted sign extension. */
 	warning (OPT_Wpointer_to_int_cast,
 		 "cast from pointer to integer of different size");


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]