Skip to contentSkip to conversation

Add -Wcast-qual flag

PATCH In review Christoph Egger 3 participants 8 messages Dec 21, 2006

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add -Wcast-qual flag

Hi!

The xen-cast.diff adds the -Wcast-qual flag.

The xen-cast-xen.diff makes the hypervisor itself buildable with the new flag
- at least on x86_32 and x86_64.
The xen-cast-tools.diff makes the tools build with the new flag.

Generally, the places with the __UNCONST() hack should be considered to be reworked. These places may hide bugs.

Christoph

Config.mk +1 -1
diff -r f58b0c778b90 Config.mk
@@ -39,7 +39,7 @@ endif
 
 CFLAGS += -std=gnu99
 
-CFLAGS += -Wall -Wstrict-prototypes
+CFLAGS += -Wall -Wstrict-prototypes -Wcast-qual
 
 # -Wunused-value makes GCC 4.x too aggressive for my taste: ignoring the
 # result of any casted expression causes a warning.
xen/arch/x86/domain_build.c +5 -4
diff -r f58b0c778b90 xen/arch/x86/domain_build.c
@@ -55,12 +55,12 @@ static long dom0_nrpages, dom0_min_nrpag
  *  If +ve: The specified amount is an absolute value.
  *  If -ve: The specified amount is subtracted from total available memory.
  */
-static long parse_amt(char *s, char **ps)
+static long parse_amt(const char *s, const char **ps)
 {
     long pages = parse_size_and_unit((*s == '-') ? s+1 : s, ps) >> PAGE_SHIFT;
     return (*s == '-') ? -pages : pages;
 }
-static void parse_dom0_mem(char *s)
+static void parse_dom0_mem(const char *s)
 {
     do {
         if ( !strncmp(s, "min:", 4) )
@@ -152,7 +152,8 @@ static void process_dom0_ioports_disable
 static void process_dom0_ioports_disable(void)
 {
     unsigned long io_from, io_to;
-    char *t, *u, *s = opt_dom0_ioports_disable;
+    char *t, *s = opt_dom0_ioports_disable;
+    const char *u;
 
     if ( *s == '\0' )
         return;
@@ -892,7 +893,7 @@ int construct_dom0(struct domain *d,
     return 0;
 }
 
-int elf_sanity_check(Elf_Ehdr *ehdr)
+int elf_sanity_check(const Elf_Ehdr *ehdr)
 {
     if ( !IS_ELF(*ehdr) ||
 #if defined(__i386__)
xen/arch/x86/hvm/platform.c +2 -1
diff -r f58b0c778b90 xen/arch/x86/hvm/platform.c
@@ -1153,7 +1153,8 @@ unsigned long copy_to_user_hvm(void *to,
         return 0;
     }
 
-    return hvm_copy_to_guest_virt((unsigned long)to, (void *)from, len);
+#define __UNCONST(x)	(void *)(unsigned long)(x)
+    return hvm_copy_to_guest_virt((unsigned long)to, __UNCONST(from), len);
 }
 
 unsigned long copy_from_user_hvm(void *to, const void *from, unsigned len)
xen/arch/x86/time.c +2 -2
diff -r f58b0c778b90 xen/arch/x86/time.c
@@ -41,7 +41,7 @@ unsigned long cpu_khz;  /* CPU clock fre
 unsigned long cpu_khz;  /* CPU clock frequency in kHz. */
 unsigned long hpet_address;
 DEFINE_SPINLOCK(rtc_lock);
-unsigned long volatile jiffies;
+volatile unsigned long jiffies;
 static u32 wc_sec, wc_nsec; /* UTC time at last 'time update'. */
 static DEFINE_SPINLOCK(wc_lock);
 
@@ -148,7 +148,7 @@ void timer_interrupt(int irq, void *dev_
     ASSERT(local_irq_is_enabled());
 
     /* Update jiffies counter. */
-    (*(unsigned long *)&jiffies)++;
+    (*(volatile unsigned long *)&jiffies)++;
 
     /* Rough hack to allow accurate timers to sort-of-work with no APIC. */
     if ( !cpu_has_apic )
xen/common/elf.c +27 -27

143 lines not on this page · raw message

xen/common/kernel.c +4 -3
diff -r f58b0c778b90 xen/common/kernel.c
@@ -20,7 +20,8 @@ int tainted;
 
 void cmdline_parse(char *cmdline)
 {
-    char opt[100], *optval, *p = cmdline, *q;
+    char opt[100], *optval, *q;
+    const char *p = cmdline;
     struct kernel_param *param;
     
     if ( p == NULL )
@@ -70,13 +71,13 @@ void cmdline_parse(char *cmdline)
                 break;
             case OPT_UINT:
                 *(unsigned int *)param->var =
-                    simple_strtol(optval, (char **)&optval, 0);
+                    simple_strtol(optval, (const char **)&optval, 0);
                 break;
             case OPT_BOOL:
                 *(int *)param->var = 1;
                 break;
             case OPT_CUSTOM:
-                ((void (*)(char *))param->var)(optval);
+                ((void (*)(const char *))param->var)(optval);
                 break;
             }
         }
xen/common/kexec.c +1 -1
diff -r f58b0c778b90 xen/common/kexec.c
@@ -37,7 +37,7 @@ spinlock_t kexec_lock = SPIN_LOCK_UNLOCK
 
 xen_kexec_reserve_t kexec_crash_area;
 
-static void __init parse_crashkernel(char *str)
+static void __init parse_crashkernel(const char *str)
 {
     unsigned long start, size;
 
xen/common/lib.c +9 -6
diff -r f58b0c778b90 xen/common/lib.c
@@ -439,11 +439,14 @@ s64 __moddi3(s64 a, s64 b)
 
 #endif /* BITS_PER_LONG == 32 */
 
-unsigned long long parse_size_and_unit(const char *s, char **ps)
-{
-	unsigned long long ret = simple_strtoull(s, (char **)&s, 0);
-
-	switch (*s) {
+unsigned long long parse_size_and_unit(const char *s, const char **ps)
+{
+	unsigned long long ret;
+	const char *s1;
+
+	ret = simple_strtoull(s, &s1, 0);
+
+	switch (*s1) {
 	case 'G': case 'g':
 		ret <<= 10;
 	case 'M': case 'm':
@@ -459,7 +462,7 @@ unsigned long long parse_size_and_unit(c
 	}
 
 	if (ps != NULL)
-		*ps = (char *)s;
+		*ps = s1;
 
 	return ret;
 }
xen/common/page_alloc.c +2 -2
diff -r f58b0c778b90 xen/common/page_alloc.c
@@ -197,7 +197,7 @@ void init_boot_pages(paddr_t ps, paddr_t
 void init_boot_pages(paddr_t ps, paddr_t pe)
 {
     unsigned long bad_spfn, bad_epfn, i;
-    char *p;
+    const char *p;
 
     ps = round_pgup(ps);
     pe = round_pgdown(pe);
@@ -586,7 +586,7 @@ void *alloc_xenheap_pages(unsigned int o
 }
 
 
-void free_xenheap_pages(void *v, unsigned int order)
+void free_xenheap_pages(const void *v, unsigned int order)
 {
     unsigned long flags;
 
xen/common/string.c +8 -6
diff -r f58b0c778b90 xen/common/string.c
@@ -7,6 +7,8 @@
 #include <xen/types.h>
 #include <xen/string.h>
 #include <xen/ctype.h>
+
+#define __UNCONST(x)	(void *)(unsigned long)(x)
 
 #ifndef __HAVE_ARCH_STRNICMP
 /**
@@ -204,7 +206,7 @@ char * strchr(const char * s, int c)
 	for(; *s != (char) c; ++s)
 		if (*s == '\0')
 			return NULL;
-	return (char *) s;
+	return __UNCONST(s);
 }
 #endif
 
@@ -219,7 +221,7 @@ char * strrchr(const char * s, int c)
        const char *p = s + strlen(s);
        do {
            if (*p == (char)c)
-               return (char *)p;
+               return __UNCONST(p);
        } while (--p >= s);
        return NULL;
 }
@@ -296,7 +298,7 @@ char * strpbrk(const char * cs,const cha
 	for( sc1 = cs; *sc1 != '\0'; ++sc1) {
 		for( sc2 = ct; *sc2 != '\0'; ++sc2) {
 			if (*sc1 == *sc2)
-				return (char *) sc1;
+				return __UNCONST(sc1);
 		}
 	}
 	return NULL;
@@ -481,12 +483,12 @@ char * strstr(const char * s1,const char
 
 	l2 = strlen(s2);
 	if (!l2)
-		return (char *) s1;
+		return __UNCONST(s1);
 	l1 = strlen(s1);
 	while (l1 >= l2) {
 		l1--;
 		if (!memcmp(s1,s2,l2))
-			return (char *) s1;
+			return __UNCONST(s1);
 		s1++;
 	}
 	return NULL;
@@ -508,7 +510,7 @@ void *memchr(const void *s, int c, size_
 	const unsigned char *p = s;
 	while (n-- != 0) {
         	if ((unsigned char)c == *p++) {
-			return (void *)(p-1);
+			return __UNCONST(p-1);
 		}
 	}
 	return NULL;
xen/common/time.c +2 -2
diff -r f58b0c778b90 xen/common/time.c
@@ -40,7 +40,7 @@ struct tm gmtime(unsigned long t)
     struct tm tbuf;
     long days, rem;
     int y;
-    unsigned short int *ip;
+    const unsigned short int *ip;
 
     days = t / SECS_PER_DAY;
     rem = t % SECS_PER_DAY;
@@ -66,7 +66,7 @@ struct tm gmtime(unsigned long t)
     }
     tbuf.tm_year = y - 1900;
     tbuf.tm_yday = days;
-    ip = (unsigned short int *)__mon_lengths[__isleap(y)];
+    ip = (const unsigned short int *)__mon_lengths[__isleap(y)];
     for ( y = 0; days >= ip[y]; ++y )
         days -= ip[y];
     tbuf.tm_mon = y;
xen/common/vsprintf.c +6 -6
diff -r f58b0c778b90 xen/common/vsprintf.c
@@ -28,7 +28,7 @@
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long simple_strtoul(const char *cp,char **endp,unsigned int base)
+unsigned long simple_strtoul(const char *cp,const char **endp,unsigned int base)
 {
     unsigned long result = 0,value;
 
@@ -52,7 +52,7 @@ unsigned long simple_strtoul(const char 
         cp++;
     }
     if (endp)
-        *endp = (char *)cp;
+        *endp = cp;
     return result;
 }
 
@@ -64,7 +64,7 @@ EXPORT_SYMBOL(simple_strtoul);
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-long simple_strtol(const char *cp,char **endp,unsigned int base)
+long simple_strtol(const char *cp,const char **endp,unsigned int base)
 {
     if(*cp=='-')
         return -simple_strtoul(cp+1,endp,base);
@@ -79,7 +79,7 @@ EXPORT_SYMBOL(simple_strtol);
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-unsigned long long simple_strtoull(const char *cp,char **endp,unsigned int base)
+unsigned long long simple_strtoull(const char *cp,const char **endp,unsigned int base)
 {
     unsigned long long result = 0,value;
 
@@ -103,7 +103,7 @@ unsigned long long simple_strtoull(const
         cp++;
     }
     if (endp)
-        *endp = (char *)cp;
+        *endp = cp;
     return result;
 }
 
@@ -115,7 +115,7 @@ EXPORT_SYMBOL(simple_strtoull);
  * @endp: A pointer to the end of the parsed string will be placed here
  * @base: The number base to use
  */
-long long simple_strtoll(const char *cp,char **endp,unsigned int base)
+long long simple_strtoll(const char *cp,const char **endp,unsigned int base)
 {
     if(*cp=='-')
         return -simple_strtoull(cp+1,endp,base);
xen/common/xmalloc.c +2 -2
diff -r f58b0c778b90 xen/common/xmalloc.c
@@ -87,7 +87,7 @@ static void *data_from_header(struct xma
 #endif
 }
 
-static struct xmalloc_hdr *header_from_data(const void *p)
+static struct xmalloc_hdr *header_from_data(void *p)
 {
 #if XMALLOC_DEBUG
     unsigned char *data = (unsigned char *)p - SMP_CACHE_BYTES;
@@ -208,7 +208,7 @@ void *_xmalloc(size_t size, size_t align
     return xmalloc_new_page(size);
 }
 
-void xfree(const void *p)
+void xfree(void *p)
 {
     unsigned long flags;
     struct xmalloc_hdr *i, *tmp, *hdr;
xen/drivers/char/ns16550.c +1 -1
diff -r f58b0c778b90 xen/drivers/char/ns16550.c
@@ -300,7 +300,7 @@ static int parse_parity_char(int c)
         return;                              \
     } while ( 0 )
 
-static void ns16550_parse_port_config(struct ns16550 *uart, char *conf)
+static void ns16550_parse_port_config(struct ns16550 *uart, const char *conf)
 {
     int baud;
 
xen/include/asm-x86/bitops.h +5 -5
diff -r f58b0c778b90 xen/include/asm-x86/bitops.h
@@ -246,7 +246,7 @@ static __inline__ int constant_test_bit(
 	return ((1U << (nr & 31)) & (((const volatile unsigned int *) addr)[nr >> 5])) != 0;
 }
 
-static __inline__ int variable_test_bit(int nr, const volatile void * addr)
+static __inline__ int variable_test_bit(int nr, volatile void * addr)
 {
 	int oldbit;
 
@@ -288,7 +288,7 @@ static inline unsigned int __scanbit(uns
  */
 #define find_first_bit(addr,size) \
 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
-  (__scanbit(*(unsigned long *)addr)) : \
+  (__scanbit(*(const unsigned long *)addr)) : \
   __find_first_bit(addr,size)))
 
 /**
@@ -299,7 +299,7 @@ static inline unsigned int __scanbit(uns
  */
 #define find_next_bit(addr,size,off) \
 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
-  ((off) + (__scanbit((*(unsigned long *)addr) >> (off)))) : \
+  ((off) + (__scanbit((*(const unsigned long *)addr) >> (off)))) : \
   __find_next_bit(addr,size,off)))
 
 /**
@@ -312,7 +312,7 @@ static inline unsigned int __scanbit(uns
  */
 #define find_first_zero_bit(addr,size) \
 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
-  (__scanbit(~*(unsigned long *)addr)) : \
+  (__scanbit(~*(const unsigned long *)addr)) : \
   __find_first_zero_bit(addr,size)))
 
 /**
@@ -323,7 +323,7 @@ static inline unsigned int __scanbit(uns
  */
 #define find_next_zero_bit(addr,size,off) \
 ((__builtin_constant_p(size) && (size) <= BITS_PER_LONG ? \
-  ((off)+(__scanbit(~(((*(unsigned long *)addr)) >> (off))))) : \
+  ((off)+(__scanbit(~(((*(const unsigned long *)addr)) >> (off))))) : \
   __find_next_zero_bit(addr,size,off)))
 
 
xen/include/asm-x86/system.h +1 -1
diff -r f58b0c778b90 xen/include/asm-x86/system.h
@@ -19,7 +19,7 @@
 #define xchg(ptr,v) ((__typeof__(*(ptr)))__xchg((unsigned long)(v),(ptr),sizeof(*(ptr))))
 
 struct __xchg_dummy { unsigned long a[100]; };
-#define __xg(x) ((struct __xchg_dummy *)(x))
+#define __xg(x) ((volatile struct __xchg_dummy *)(x))
 
 
 /*
xen/include/asm-x86/uaccess.h +5 -5
diff -r f58b0c778b90 xen/include/asm-x86/uaccess.h
@@ -140,7 +140,7 @@ extern void __put_user_bad(void);
 })							
 
 struct __large_struct { unsigned long buf[100]; };
-#define __m(x) (*(struct __large_struct *)(x))
+#define __m(x) (*(const struct __large_struct *)(x))
 
 /*
  * Tell gcc we read from memory instead of writing: this is because
@@ -200,16 +200,16 @@ __copy_to_user(void __user *to, const vo
 
         switch (n) {
         case 1:
-            __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1);
+            __put_user_size(*(const u8 *)from, (u8 __user *)to, 1, ret, 1);
             return ret;
         case 2:
-            __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2);
+            __put_user_size(*(const u16 *)from, (u16 __user *)to, 2, ret, 2);
             return ret;
         case 4:
-            __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4);
+            __put_user_size(*(const u32 *)from, (u32 __user *)to, 4, ret, 4);
             return ret;
         case 8:
-            __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8);
+            __put_user_size(*(const u64 *)from, (u64 __user *)to, 8, ret, 8);
             return ret;
         }
     }
xen/include/xen/elf.h +1 -1
diff -r f58b0c778b90 xen/include/xen/elf.h
@@ -526,7 +526,7 @@ extern const char *xen_elfnote_string(st
 extern const char *xen_elfnote_string(struct domain_setup_info *dsi, int type);
 
 #ifdef Elf_Ehdr
-extern int elf_sanity_check(Elf_Ehdr *ehdr);
+extern int elf_sanity_check(const Elf_Ehdr *ehdr);
 #endif
 
 #endif /* __XEN_ELF_H__ */
xen/include/xen/kernel.h +1 -1
diff -r f58b0c778b90 xen/include/xen/kernel.h
@@ -42,7 +42,7 @@
  *
  */
 #define container_of(ptr, type, member) ({			\
-        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
         (type *)( (char *)__mptr - offsetof(type,member) );})
 
 /*
xen/include/xen/lib.h +5 -5
diff -r f58b0c778b90 xen/include/xen/lib.h
@@ -74,15 +74,15 @@ extern int vscnprintf(char *buf, size_t 
     __attribute__ ((format (printf, 3, 0)));
 
 long simple_strtol(
-    const char *cp,char **endp, unsigned int base);
+    const char *cp,const char **endp, unsigned int base);
 unsigned long simple_strtoul(
-    const char *cp,char **endp, unsigned int base);
+    const char *cp,const char **endp, unsigned int base);
 long long simple_strtoll(
-    const char *cp,char **endp, unsigned int base);
+    const char *cp,const char **endp, unsigned int base);
 unsigned long long simple_strtoull(
-    const char *cp,char **endp, unsigned int base);
+    const char *cp,const char **endp, unsigned int base);
 
-unsigned long long parse_size_and_unit(const char *s, char **ps);
+unsigned long long parse_size_and_unit(const char *s, const char **ps);
 
 #define TAINT_UNSAFE_SMP                (1<<0)
 #define TAINT_MACHINE_CHECK             (1<<1)
xen/include/xen/mm.h +1 -1
diff -r f58b0c778b90 xen/include/xen/mm.h
@@ -55,7 +55,7 @@ void scrub_heap_pages(void);
 /* Xen suballocator. These functions are interrupt-safe. */
 void init_xenheap_pages(paddr_t ps, paddr_t pe);
 void *alloc_xenheap_pages(unsigned int order);
-void free_xenheap_pages(void *v, unsigned int order);
+void free_xenheap_pages(const void *v, unsigned int order);
 #define alloc_xenheap_page() (alloc_xenheap_pages(0))
 #define free_xenheap_page(v) (free_xenheap_pages(v,0))
 
xen/include/xen/sched.h +2 -2
diff -r f58b0c778b90 xen/include/xen/sched.h
@@ -202,8 +202,8 @@ struct domain_setup_info
      * You should use the xen_elfnote_* accessors below in order to
      * pickup the correct one and retain backwards compatibility.
      */
-    void *__elfnote_section, *__elfnote_section_end;
-    char *__xen_guest_string;
+    const void *__elfnote_section, *__elfnote_section_end;
+    const char *__xen_guest_string;
 };
 
 extern struct vcpu *idle_vcpu[NR_CPUS];
xen/include/xen/xmalloc.h +1 -1
diff -r f58b0c778b90 xen/include/xen/xmalloc.h
@@ -12,7 +12,7 @@
 #define xmalloc_bytes(_bytes) (_xmalloc(_bytes, SMP_CACHE_BYTES))
 
 /* Free any of the above. */
-extern void xfree(const void *);
+extern void xfree(void *);
 
 /* Underlying functions */
 extern void *_xmalloc(size_t size, size_t align);
tools/blktap/drivers/block-qcow.c +4 -2
diff -r f58b0c778b90 tools/blktap/drivers/block-qcow.c
@@ -38,6 +38,8 @@
 #include "bswap.h"
 #include "aes.h"
 #include "tapdisk.h"
+
+#define __UNCONST(x)	(void *)(unsigned long)(x)
 
 #if 1
 #define ASSERT(_p) \
@@ -759,7 +761,7 @@ static int decompress_buffer(uint8_t *ou
 	
 	memset(strm, 0, sizeof(*strm));
 	
-	strm->next_in = (uint8_t *)buf;
+	strm->next_in = (uint8_t *)__UNCONST(buf);
 	strm->avail_in = buf_size;
 	strm->next_out = out_buf;
 	strm->avail_out = out_buf_size;
@@ -1445,7 +1447,7 @@ int qcow_compress_cluster(struct td_stat
 	}
 
 	strm.avail_in = s->cluster_size;
-	strm.next_in = (uint8_t *)buf;
+	strm.next_in = (uint8_t *)__UNCONST(buf);
 	strm.avail_out = s->cluster_size;
 	strm.next_out = out_buf;
 
tools/libxc/xc_linux_build.c +1 -1
diff -r f58b0c778b90 tools/libxc/xc_linux_build.c
@@ -1079,7 +1079,7 @@ static int xc_linux_build_internal(int x
 static int xc_linux_build_internal(int xc_handle,
                                    uint32_t domid,
                                    unsigned int mem_mb,
-                                   char *image,
+                                   const char *image,
                                    unsigned long image_size,
                                    struct initrd_info *initrd,
                                    const char *cmdline,
tools/libxc/xc_linux_save.c +5 -5
diff -r f58b0c778b90 tools/libxc/xc_linux_save.c
@@ -80,7 +80,7 @@ static xen_pfn_t *live_m2p = NULL;
 #define BITMAP_SIZE   ((max_pfn + BITS_PER_LONG - 1) / 8)
 
 #define BITMAP_ENTRY(_nr,_bmap) \
-   ((unsigned long *)(_bmap))[(_nr)/BITS_PER_LONG]
+   ((volatile unsigned long *)(_bmap))[(_nr)/BITS_PER_LONG]
 
 #define BITMAP_SHIFT(_nr) ((_nr) % BITS_PER_LONG)
 
@@ -112,7 +112,7 @@ static inline int count_bits ( int nr, v
 static inline int count_bits ( int nr, volatile void *addr)
 {
     int i, count = 0;
-    unsigned long *p = (unsigned long *)addr;
+    volatile unsigned long *p = (volatile unsigned long *)addr;
     /* We know that the array is padded to unsigned long. */
     for( i = 0; i < (nr / (sizeof(unsigned long)*8)); i++, p++ )
         count += hweight32(*p);
@@ -443,7 +443,7 @@ static int canonicalize_pagetable(unsign
 
 /* XXX index of the L2 entry in PAE mode which holds the guest LPT */
 #define PAE_GLPT_L2ENTRY (495)
-        pte = ((uint64_t*)spage)[PAE_GLPT_L2ENTRY];
+        pte = ((const uint64_t*)spage)[PAE_GLPT_L2ENTRY];
 
         if(((pte >> PAGE_SHIFT) & 0x0fffffff) == live_p2m[pfn])
             xen_start = (hvirt_start >> L2_PAGETABLE_SHIFT_PAE) & 0x1ff;
@@ -464,9 +464,9 @@ static int canonicalize_pagetable(unsign
         unsigned long pfn, mfn;
 
         if (pt_levels == 2)
-            pte = ((uint32_t*)spage)[i];
+            pte = ((const uint32_t*)spage)[i];
         else
-            pte = ((uint64_t*)spage)[i];
+            pte = ((const uint64_t*)spage)[i];
 
         if (i >= xen_start && i < xen_end)
             pte = 0;
tools/libxc/xc_load_bin.c +13 -13
diff -r f58b0c778b90 tools/libxc/xc_load_bin.c
@@ -98,7 +98,7 @@ struct xen_bin_image_table
 #define FLAGS_MASK     ((~ 0) & (~ XEN_REACTOS_FLAG_ALIGN4K))
 #define FLAGS_REQUIRED XEN_REACTOS_FLAG_ADDRSVALID
 
-static struct xen_bin_image_table *
+static const struct xen_bin_image_table *
 findtable(const char *image, unsigned long image_size);
 static int
 parsebinimage(
@@ -122,11 +122,11 @@ int probe_bin(const char *image,
     return 0;
 }
 
-static struct xen_bin_image_table *
+static const struct xen_bin_image_table *
 findtable(const char *image, unsigned long image_size)
 {
-    struct xen_bin_image_table *table;
-    unsigned long *probe_ptr;
+    const struct xen_bin_image_table *table;
+    const unsigned long *probe_ptr;
     unsigned probe_index;
     unsigned probe_count;
 
@@ -142,13 +142,13 @@ findtable(const char *image, unsigned lo
                   sizeof(unsigned long);
 
     /* Search for the magic header */
-    probe_ptr = (unsigned long *) image;
+    probe_ptr = (const unsigned long *) image;
     table = NULL;
     for ( probe_index = 0; probe_index < probe_count; probe_index++ )
     {
         if ( XEN_REACTOS_MAGIC3 == *probe_ptr )
         {
-            table = (struct xen_bin_image_table *) probe_ptr;
+            table = (const struct xen_bin_image_table *) probe_ptr;
             /* Checksum correct? */
             if ( 0 == table->magic + table->flags + table->checksum )
             {
@@ -165,7 +165,7 @@ static int parsebinimage(const char *ima
                          unsigned long image_size,
                          struct domain_setup_info *dsi)
 {
-    struct xen_bin_image_table *image_info;
+    const struct xen_bin_image_table *image_info;
     unsigned long start_addr;
     unsigned long end_addr;
 
@@ -186,13 +186,13 @@ static int parsebinimage(const char *ima
 
     /* Sanity check on the addresses */
     if ( image_info->header_addr < image_info->load_addr ||
-         ((char *) image_info - image) <
+         ((const char *) image_info - image) <
          (image_info->header_addr - image_info->load_addr) )
     {
         ERROR("Invalid header_addr.");
         return -EINVAL;
     }
-    start_addr = image_info->header_addr - ((char *) image_info - image);
+    start_addr = image_info->header_addr - ((const char *) image_info - image);
     if ( 0 != image_info->load_end_addr &&
          ( image_info->load_end_addr < image_info->load_end_addr ||
            start_addr + image_size < image_info->load_end_addr ) )
@@ -221,7 +221,7 @@ static int parsebinimage(const char *ima
     else
     {
         dsi->v_end = image_info->load_addr + image_size -
-                     (((char *) image_info - image) -
+                     (((const char *) image_info - image) -
                       (image_info->header_addr - image_info->load_addr));
     }
     dsi->v_kernstart = dsi->v_start;
@@ -240,7 +240,7 @@ loadbinimage(
     unsigned long size;
     char         *va;
     unsigned long done, chunksz;
-    struct xen_bin_image_table *image_info;
+    const struct xen_bin_image_table *image_info;
 
     image_info = findtable(image, image_size);
     if ( NULL == image_info )
@@ -252,7 +252,7 @@ loadbinimage(
     /* Determine image size */
     if ( 0 == image_info->load_end_addr )
     {
-        size = image_size  - (((char *) image_info - image) -
+        size = image_size  - (((const char *)image_info - image) -
                               (image_info->header_addr -
                                image_info->load_addr));
     }
@@ -262,7 +262,7 @@ loadbinimage(
     }
 
     /* It's possible that we need to skip the first part of the image */
-    image += ((char *)image_info - image) -
+    image += ((const char *)image_info - image) -
              (image_info->header_addr - image_info->load_addr);
 
     for ( done = 0; done < size; done += chunksz )
tools/libxc/xc_load_elf.c +39 -38

208 lines not on this page · raw message

tools/libxc/xg_private.c +3 -2
diff -r f58b0c778b90 tools/libxc/xg_private.c
@@ -11,6 +11,7 @@
 
 #include "xg_private.h"
 
+#define __UNCONST(x)	(void *)(unsigned long)(x)
 int lock_pages(void *addr, size_t len)
 {
     int e = 0;
@@ -116,7 +117,7 @@ char *xc_inflate_buffer(const char *in_b
     {
         if ( out_size != NULL )
             *out_size = in_size;
-        return (char *)in_buf;
+        return __UNCONST(in_buf);
     }
 
     out_len = (unsigned char)in_buf[in_size-4] +
@@ -132,7 +133,7 @@ char *xc_inflate_buffer(const char *in_b
         return NULL;
     }
 
-    zStream.next_in = (unsigned char *)in_buf;
+    zStream.next_in = (unsigned char *)__UNCONST(in_buf);
     zStream.avail_in = in_size;
     zStream.next_out = (unsigned char *)out_buf;
     zStream.avail_out = out_len+16;
tools/libxc/xg_private.h +3 -3
diff -r f58b0c778b90 tools/libxc/xg_private.h
@@ -146,7 +146,7 @@ struct domain_setup_info
      * You should use the xen_elfnote_* accessors below in order to
      * pickup the correct one and retain backwards compatibility.
      */
-    void *__elfnote_section, *__elfnote_section_end;
+    const void *__elfnote_section, *__elfnote_section_end;
     const char *__xen_guest_string;
 };
 
@@ -162,14 +162,14 @@ typedef int (*loadimagefunc)(const char 
  * in the note is returned and *defined is set to non-zero. If no such
  * note is found then *defined is set to 0 and 0 is returned.
  */
-extern unsigned long long xen_elfnote_numeric(struct domain_setup_info *dsi,
+extern unsigned long long xen_elfnote_numeric(const struct domain_setup_info *dsi,
 					      int type, int *defined);
 
 /*
  * If an ELF note of the given type is found then the string contained
  * in the value is returned, otherwise NULL is returned.
  */
-extern const char * xen_elfnote_string(struct domain_setup_info *dsi,
+extern const char * xen_elfnote_string(const struct domain_setup_info *dsi,
 				       int type);
 
 struct load_funcs
tools/xenstat/xentop/xentop.c +2 -2
diff -r f58b0c778b90 tools/xenstat/xentop/xentop.c
@@ -269,7 +269,7 @@ static void print(const char *fmt, ...)
 	if (!batch) {
 		if((current_row() < lines()-1)) {
 			va_start(args, fmt);
-			vwprintw(stdscr, (char *)fmt, args);
+			vwprintw(stdscr, (const char *)fmt, args);
 			va_end(args);
 		}
 	} else {
@@ -283,7 +283,7 @@ static void attr_addstr(int attr, const 
 static void attr_addstr(int attr, const char *str)
 {
 	attron(attr);
-	addstr((char *)str);
+	addstr((const char *)str);
 	attroff(attr);
 }
 
tools/xenstore/list.h +1 -1
diff -r f58b0c778b90 tools/xenstore/list.h
@@ -12,7 +12,7 @@
 #define LIST_POISON2  ((void *) 0x00200200)
 
 #define container_of(ptr, type, member) ({			\
-        const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
+        typeof( ((type *)0)->member ) *__mptr = (ptr);	\
         (type *)( (char *)__mptr - offsetof(type,member) );})
 
 /*
tools/xenstore/xenstored_core.c +8 -7
diff -r f58b0c778b90 tools/xenstore/xenstored_core.c
@@ -53,6 +53,7 @@
 
 #include "hashtable.h"
 
+#define __UNCONST(x)	(void *)(unsigned long)(x)
 
 extern int xce_handle; /* in xenstored_domain.c */
 
@@ -417,7 +418,7 @@ static struct node *read_node(struct con
 	struct node *node;
 	TDB_CONTEXT * context = tdb_context(conn);
 
-	key.dptr = (void *)name;
+	key.dptr = (char *)__UNCONST(name);
 	key.dsize = strlen(name);
 	data = tdb_fetch(context, key);
 
@@ -463,7 +464,7 @@ static bool write_node(struct connection
 	TDB_DATA key, data;
 	void *p;
 
-	key.dptr = (void *)node->name;
+	key.dptr = (char *)__UNCONST(node->name);
 	key.dsize = strlen(node->name);
 
 	data.dsize = 3*sizeof(uint32_t)
@@ -736,11 +737,11 @@ char *canonicalize(struct connection *co
 	const char *prefix;
 
 	if (!node || strstarts(node, "/"))
-		return (char *)node;
+		return __UNCONST(node); 
 	prefix = get_implicit_path(conn);
 	if (prefix)
 		return talloc_asprintf(node, "%s/%s", prefix, node);
-	return (char *)node;
+	return __UNCONST(node); 
 }
 
 bool check_event_node(const char *node)
@@ -784,7 +785,7 @@ static void delete_node_single(struct co
 {
 	TDB_DATA key;
 
-	key.dptr = (void *)node->name;
+	key.dptr = (void *)__UNCONST(node->name);
 	key.dsize = strlen(node->name);
 
 	if (tdb_delete(tdb_context(conn), key) != 0) {
@@ -854,7 +855,7 @@ static int destroy_node(void *_node)
 	if (streq(node->name, "/"))
 		corrupt(NULL, "Destroying root node!");
 
-	key.dptr = (void *)node->name;
+	key.dptr = (void *)__UNCONST(node->name);
 	key.dsize = strlen(node->name);
 
 	tdb_delete(node->tdb, key);
@@ -1446,7 +1447,7 @@ static void manual_node(const char *name
 	node->name = name;
 	node->perms = &perms;
 	node->num_perms = 1;
-	node->children = (char *)child;
+	node->children = (char *)__UNCONST(child);
 	if (child)
 		node->childlen = strlen(child) + 1;
 
tools/xenstore/xs.c +11 -9
diff -r f58b0c778b90 tools/xenstore/xs.c
@@ -77,6 +77,8 @@ struct xs_handle {
 	pthread_mutex_t request_mutex;
 };
 
+#define __UNCONST(x)	(void *)(unsigned long)(x)
+
 static int read_message(struct xs_handle *h);
 static void *read_thread(void *arg);
 
@@ -381,7 +383,7 @@ static void *xs_single(struct xs_handle 
 {
 	struct iovec iovec;
 
-	iovec.iov_base = (void *)string;
+	iovec.iov_base = (void *)__UNCONST(string);
 	iovec.iov_len = strlen(string) + 1;
 	return xs_talkv(h, t, type, &iovec, 1, len);
 }
@@ -440,9 +442,9 @@ bool xs_write(struct xs_handle *h, xs_tr
 {
 	struct iovec iovec[2];
 
-	iovec[0].iov_base = (void *)path;
+	iovec[0].iov_base = (void *)__UNCONST(path);
 	iovec[0].iov_len = strlen(path) + 1;
-	iovec[1].iov_base = (void *)data;
+	iovec[1].iov_base = (void *)__UNCONST(data);
 	iovec[1].iov_len = len;
 
 	return xs_bool(xs_talkv(h, t, XS_WRITE, iovec,
@@ -513,7 +515,7 @@ bool xs_set_permissions(struct xs_handle
 	unsigned int i;
 	struct iovec iov[1+num_perms];
 
-	iov[0].iov_base = (void *)path;
+	iov[0].iov_base = (void *)__UNCONST(path);
 	iov[0].iov_len = strlen(path) + 1;
 	
 	for (i = 0; i < num_perms; i++) {
@@ -561,9 +563,9 @@ bool xs_watch(struct xs_handle *h, const
 	}
 	pthread_mutex_unlock(&h->request_mutex);
 
-	iov[0].iov_base = (void *)path;
+	iov[0].iov_base = (void *)__UNCONST(path);
 	iov[0].iov_len = strlen(path) + 1;
-	iov[1].iov_base = (void *)token;
+	iov[1].iov_base = (void *)__UNCONST(token);
 	iov[1].iov_len = strlen(token) + 1;
 
 	return xs_bool(xs_talkv(h, XBT_NULL, XS_WATCH, iov,
@@ -628,9 +630,9 @@ bool xs_unwatch(struct xs_handle *h, con
 {
 	struct iovec iov[2];
 
-	iov[0].iov_base = (char *)path;
+	iov[0].iov_base = (char *)__UNCONST(path);
 	iov[0].iov_len = strlen(path) + 1;
-	iov[1].iov_base = (char *)token;
+	iov[1].iov_base = (char *)__UNCONST(token);
 	iov[1].iov_len = strlen(token) + 1;
 
 	return xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov,
@@ -740,7 +742,7 @@ char *xs_debug_command(struct xs_handle 
 {
 	struct iovec iov[2];
 
-	iov[0].iov_base = (void *)cmd;
+	iov[0].iov_base = (void *)__UNCONST(cmd);
 	iov[0].iov_len = strlen(cmd) + 1;
 	iov[1].iov_base = data;
 	iov[1].iov_len = len;

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel

#1 lore raw
Quoted 1 line · Christoph Egger

The xen-cast.diff adds the -Wcast-qual flag.

We don't enable this flag precisely because we end up needing to scatter const all over the place like confetti. I'm not convinced that use of const improves code quality. I fear we'd end up with 'const foo * const bar' all over the place -- ugly and confusing to most programmers (who frequently mess up const placement, in my experience).

-- Keir

#2 lore raw
Quoted 5 lines · Keir Fraser
Quoted 1 line · Christoph Egger

The xen-cast.diff adds the -Wcast-qual flag.

We don't enable this flag precisely because we end up needing to scatter const all over the place like confetti.

That doesn't imply that this is bad. Compilers may generate better code.

Quoted 1 line

I'm not convinced that use of const improves code quality.

In my experience, exactly this will happen.

Quoted 3 lines

I fear we'd end up with 'const foo * const bar' all
over the place -- ugly and confusing to most programmers (who frequently mess up const placement, in my experience).

Exactly these programmers will stop doing this, when the compiler tells them.

With -Wcast-qual you find code that does things like this:

     int foo(const char *str) {
          char *s = str;
          int i;
          ....
          s[i] = '\0';
     }

Modifying read-only memory is IMO never a good idea, in particular, when it is shared with something else.

And there are indeed places in the xen code, where this happens. (Example: Look how canonicalize() is used in tools/xenstored/*,
Look at copy_to_user_hvm() in xen/arch/x86/hvm/platform.c )

Please have a closer look at the patches.

#3 lore raw
In reply to Keir Fraser
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 21.12.06 11:41 >>>

Quoted 1 line · Christoph Egger

The xen-cast.diff adds the -Wcast-qual flag.

We don't enable this flag precisely because we end up needing to scatter const all over the place like confetti. I'm not convinced that use of const improves code quality. I fear we'd end up with 'const foo * const bar' all over the place -- ugly and confusing to most programmers (who frequently mess up const placement, in my experience).

While I'd really like to see all Xen code become const-correct (because I do believe this helps with code quality, like does marking read only data read only in the page tables), I don't think adding the warning here is appropriate - after all, in C this is one of the purposes of adding explicit casts (although I agree it's questionable to use casts for this purpose). And no, I don't think that many people screwing up const placement is a proper argument for not using const where possible.

Jan

#4 lore raw
Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 21.12.06 11:41 >>>

Quoted 1 line · Christoph Egger

The xen-cast.diff adds the -Wcast-qual flag.

We don't enable this flag precisely because we end up needing to scatter const all over the place like confetti. I'm not convinced that use of const improves code quality. I fear we'd end up with 'const foo * const bar' all over the place -- ugly and confusing to most programmers (who frequently mess up const placement, in my experience).

Quoted 4 lines

While I'd really like to see all Xen code become const-correct (because I do believe this helps with code quality, like does marking read only data read only in the page tables), I don't think adding the warning here is appropriate

IMO, it is. It is one step towards to do more type-safe coding.

Quoted 4 lines

- after all, in C this is one of the purposes of adding explicit casts (although I agree it's questionable to use casts for this purpose). And no, I don't think that many people screwing up const placement is a proper argument for not using const where possible.

Full ack.

Christoph

#5 lore raw
In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

While I'd really like to see all Xen code become const-correct (because I do believe this helps with code quality, like does marking read only data read only in the page tables), I don't think adding the warning here is appropriate - after all, in C this is one of the purposes of adding explicit casts (although I agree it's questionable to use casts for this purpose). And no, I don't think that many people screwing up const placement is a proper argument for not using const where possible.

Well, I'm inclined to take the clean-up parts of the patches but leave out __UNCONST() and -Wcast-qual. I really dislike the compiler thinking it knows better than an explicit cast, and casting via unsigned long is just a kludge.

-- Keir

#6 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 21.12.06 14:28 >>>

Quoted 7 lines · Jan Beulich

While I'd really like to see all Xen code become const-correct (because I do believe this helps with code quality, like does marking read only data read only in the page tables), I don't think adding the warning here is appropriate - after all, in C this is one of the purposes of adding explicit casts (although I agree it's questionable to use casts for this purpose). And no, I don't think that many people screwing up const placement is a proper argument for not using const where possible.

Quoted 4 lines

Well, I'm inclined to take the clean-up parts of the patches but leave out __UNCONST() and -Wcast-qual. I really dislike the compiler thinking it knows better than an explicit cast, and casting via unsigned long is just a kludge.

This I completely agree with.

Jan

#7 lore raw
Quoted 11 lines · Jan Beulich
Quoted 11 lines
Quoted 1 line
Quoted 1 line

Keir Fraser <[email protected]> 21.12.06 14:28 >>>

Quoted 7 lines · Jan Beulich

While I'd really like to see all Xen code become const-correct (because I do believe this helps with code quality, like does marking read only data read only in the page tables), I don't think adding the warning here is appropriate - after all, in C this is one of the purposes of adding explicit casts (although I agree it's questionable to use casts for this purpose). And no, I don't think that many people screwing up const placement is a proper argument for not using const where possible.

Quoted 6 lines
Quoted 4 lines

Well, I'm inclined to take the clean-up parts of the patches but leave out __UNCONST() and -Wcast-qual. I really dislike the compiler thinking it knows better than an explicit cast, and casting via unsigned long is just a kludge.

This I completely agree with.

Me, too. I mentioned in my first mail, these parts with __UNCONST() must be reworked to get rid of it. There was no obvious solution for me at least. If someone knows a way, I am for it.