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 on memset with constant length 0


This is patch to warn when memset is being called with a constant
length 0.  I tested this on i686-pc-linux-gnu.

gcc/ChangeLog:

2006-12-03  Assar Westerlund  <assar@kth.se>

	* builtins.c (fold_builtin_memset): Warn about memset with length
	== 0 with -Wextra.

gcc/testsuite/ChangeLog:

2006-12-03  Assar Westerlund  <assar@kth.se>

	* gcc.dg/memset-constant0-1.c: Test for "memset with 0" warning.
	* gcc.dg/memset-constant0-2.c: Likewise.

Index: gcc/builtins.c
===================================================================
--- gcc/builtins.c	(revision 119393)
+++ gcc/builtins.c	(working copy)
@@ -8051,8 +8051,13 @@
 
   /* If the LEN parameter is zero, return DEST.  */
   if (integer_zerop (len))
-    return omit_one_operand (type, dest, c);
+    {
+      if (extra_warnings)
+        warning (OPT_Wextra, "memset called with a constant 0 length");
 
+      return omit_one_operand (type, dest, c);
+    }
+
   if (! host_integerp (c, 1) || TREE_SIDE_EFFECTS (dest))
     return 0;
 
Index: gcc/testsuite/gcc.dg/memset-constant0-2.c
===================================================================
--- gcc/testsuite/gcc.dg/memset-constant0-2.c	(revision 0)
+++ gcc/testsuite/gcc.dg/memset-constant0-2.c	(revision 0)
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-Wextra" } */
+
+typedef unsigned size_t;
+void *memset(void *s, int c, size_t n);
+
+void clear(void *p, size_t len)
+{
+  memset(p, len, 0); /* { dg-warning "memset called with a constant 0 length" } */
+}
Index: gcc/testsuite/gcc.dg/memset-constant0-1.c
===================================================================
--- gcc/testsuite/gcc.dg/memset-constant0-1.c	(revision 0)
+++ gcc/testsuite/gcc.dg/memset-constant0-1.c	(revision 0)
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Wextra" } */
+
+typedef unsigned size_t;
+void *memset(void *s, int c, size_t n);
+
+int main()
+{
+  int foo;
+  memset(&foo, sizeof(foo), 0); /* { dg-warning "memset called with a constant 0 length" } */
+  return foo;
+}


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