This is the mail archive of the gcc-bugs@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]

/usr/include/bits/string2.h vs. `gcc -Wpointer-arith'


If I compile XEmacs on a Mandrake 7.0 Linux system, based on glibc
2.1, I get these warnings:

/hack/import/gcc-2.95.2/inst/linux-libc2/bin/gcc -c -march=k6 -fstrict-aliasing -O3 -Wall -Winline -Wmissing-prototypes -Wmissing-declarations -Wno-switch -Wpointer-arith -Wno-sign-compare -Wshadow  -Demacs -I. -DHAVE_CONFIG_H -I/usr/X11R6/include /xemacs/ws/dev/src/alloc.c
/usr/include/bits/string2.h: In function `__mempcpy_small':
/usr/include/bits/string2.h:238: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:242: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:246: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:248: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:252: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:256: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:258: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:262: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:264: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:268: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:270: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:272: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:276: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:278: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h: In function `__strcpy_small':
/usr/include/bits/string2.h:419: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:427: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:432: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:437: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:439: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:444: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h: In function `__stpcpy_small':
/usr/include/bits/string2.h:575: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:579: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:584: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:588: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:593: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:595: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:599: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:601: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:606: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:608: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h: In function `__mempcpy_small':
/usr/include/bits/string2.h:238: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:242: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:246: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:248: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:252: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:256: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:258: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:262: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:264: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:268: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:270: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:272: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:276: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:278: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h: In function `__strcpy_small':
/usr/include/bits/string2.h:419: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:427: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:432: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:437: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:439: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:444: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h: In function `__stpcpy_small':
/usr/include/bits/string2.h:575: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:579: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:584: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:588: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:593: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:595: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:599: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:601: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:606: warning: pointer of type `void *' used in arithmetic
/usr/include/bits/string2.h:608: warning: pointer of type `void *' used in arithmetic

This is despite the use of __extension__ around the void * pointer
addition construct.  I'm not sure whether this should be considered a
gcc bug as well, but I think you should eliminate the warnings in any
case.  The construct can be rewritten in portable C as follows.  I
don't know whether this code is used with C++, but the construct below
is also portable C++ - there are no uncast conversions from void *.

(My apologies for not generating a patch against the latest CVS.  I'm
too frightened to try to _install_ libc.)

If you accept this patch in principle, I would be willing to generate
a proper patch against libc CVS.

ObLegal:  I hereby place this article in the public domain.

(martin@wobble) /usr/include/bits $ diff -u string2.h.orig string2.h
--- string2.h.orig	Wed Dec 15 07:46:39 1999
+++ string2.h	Fri Mar 10 10:03:21 2000
@@ -37,7 +37,7 @@
    to make sure we don't get redefinitions.
 
    We must use here macros instead of inline functions since the
-   trick won't work with the later.  */
+   trick won't work with the latter.  */
 
 #ifndef __STRING_INLINE
 # ifdef __cplusplus
@@ -110,11 +110,12 @@
 
 #  define __memset_gc(s, c, n) \
   ({ void *__s = (s);							      \
-     union {								      \
+     typedef union {							      \
        unsigned int __ui;						      \
        unsigned short int __usi;					      \
        unsigned char __uc;						      \
-     } *__u = __s;							      \
+     } * __typeof__u;							      \
+     __typeof__u __u = (__typeof__u) __s;				      \
      __uint8_t __c = (__uint8_t) (c);					      \
 									      \
      /* This `switch' statement will be removed at compile-time.  */	      \
@@ -122,54 +123,54 @@
        {								      \
        case 15:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 11:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 7:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 3:								      \
 	 __u->__usi = (unsigned short int) __c * 0x0101;		      \
-	 __u = __extension__ ((void *) __u + 2);			      \
+	 __u = (__typeof__u) ((char *) __u + 2);			      \
 	 __u->__uc = (unsigned char) __c;				      \
 	 break;								      \
 									      \
        case 14:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 10:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 6:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 2:								      \
 	 __u->__usi = (unsigned short int) __c * 0x0101;		      \
 	 break;								      \
 									      \
        case 13:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 9:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 5:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 1:								      \
 	 __u->__uc = (unsigned char) __c;				      \
 	 break;								      \
 									      \
        case 16:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 12:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 8:								      \
 	 __u->__ui = __c * 0x01010101;					      \
-	 __u = __extension__ ((void *) __u + 4);			      \
+	 __u = (__typeof__u) ((char *) __u + 4);			      \
        case 4:								      \
 	 __u->__ui = __c * 0x01010101;					      \
        case 0:								      \
@@ -225,57 +226,58 @@
 		 __uint32_t __src0_4, __uint32_t __src4_4,
 		 size_t __srclen)
 {
-  union {
+  typedef union {
     __uint32_t __ui;
     __uint16_t __usi;
     unsigned char __uc;
     unsigned char __c;
-  } *__u = __dest1;
+  } * __typeof__u;
+  __typeof__u __u = (__typeof__u) __dest1;
   switch (__srclen)
     {
     case 1:
       __u->__c = __src0_1;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 2:
       __u->__usi = __src0_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       break;
     case 3:
       __u->__usi = __src0_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__c = __src2_1;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 4:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       break;
     case 5:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__c = __src4_1;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 6:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       break;
     case 7:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__c = __src6_1;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 8:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__ui = __src4_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       break;
     }
   return (void *) __u;
@@ -360,7 +362,7 @@
       __extension__ __u->__sca8 = __src8;
       break;
     }
-  return __extension__ ((void *) __u + __srclen);
+  return (void *) ((char *) __u + __srclen);
 }
 #  endif
 # endif
@@ -401,11 +403,12 @@
 		__uint32_t __src0_4, __uint32_t __src4_4,
 		size_t __srclen)
 {
-  union {
+  typedef union {
     __uint32_t __ui;
     __uint16_t __usi;
     unsigned char __uc;
-  } *__u = (void *) __dest;
+  } * __typeof__u;
+  __typeof__u __u = (__typeof__u) __dest;
   switch (__srclen)
     {
     case 1:
@@ -416,7 +419,7 @@
       break;
     case 3:
       __u->__usi = __src0_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__uc = '\0';
       break;
     case 4:
@@ -424,24 +427,24 @@
       break;
     case 5:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__uc = '\0';
       break;
     case 6:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
       break;
     case 7:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__uc = '\0';
       break;
     case 8:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__ui = __src4_4;
       break;
     }
@@ -560,11 +563,12 @@
 		__uint32_t __src0_4, __uint32_t __src4_4,
 		size_t __srclen)
 {
-  union {
+  typedef union {
     unsigned int __ui;
     unsigned short int __usi;
     unsigned char __uc;
-  } *__u = (void *) __dest;
+  } * __typeof__u;
+  __typeof__u __u = (__typeof__u) __dest;
   switch (__srclen)
     {
     case 1:
@@ -572,40 +576,40 @@
       break;
     case 2:
       __u->__usi = __src0_2;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 3:
       __u->__usi = __src0_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__uc = '\0';
       break;
     case 4:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 3);
+      __u = (__typeof__u) ((char *) __u + 3);
       break;
     case 5:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__uc = '\0';
       break;
     case 6:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
-      __u = __extension__ ((void *) __u + 1);
+      __u = (__typeof__u) ((char *) __u + 1);
       break;
     case 7:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__usi = __src4_2;
-      __u = __extension__ ((void *) __u + 2);
+      __u = (__typeof__u) ((char *) __u + 2);
       __u->__uc = '\0';
       break;
     case 8:
       __u->__ui = __src0_4;
-      __u = __extension__ ((void *) __u + 4);
+      __u = (__typeof__u) ((char *) __u + 4);
       __u->__ui = __src4_4;
-      __u = __extension__ ((void *) __u + 3);
+      __u = (__typeof__u) ((char *) __u + 3);
       break;
     }
   return (char *) &__u->__uc;

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