Skip to contentSkip to conversation

dtc: Enable and fix -Wpointer-arith warnings

PATCH David Gibson 2 participants 2 messages Jul 7, 2008 – Jul 14, 2008 Kbuild

lore
Share & tools
lore
#0 lore raw

This patch turns on the -Wpointer-arith option in the dtc Makefile, and fixes the resulting warnings due to using (void *) in pointer arithmetic. While convenient, pointer arithmetic on void * is not portable, so it's better that we avoid it, particularly in libfdt.

Makefile +1 -1
Index: dtc/Makefile
===================================================================
@@ -16,7 +16,7 @@
 CONFIG_LOCALVERSION =
 
 CPPFLAGS = -I libfdt
-CFLAGS = -Wall -g -Os
+CFLAGS = -Wall -g -Os -Wpointer-arith
 
 CPPFLAGS += -std=c99 -D_XOPEN_SOURCE -D_BSD_SOURCE
 CFLAGS += -Werror
libfdt/fdt.c +1 -1
Index: dtc/libfdt/fdt.c
===================================================================
@@ -76,7 +76,7 @@
 
 const void *fdt_offset_ptr(const void *fdt, int offset, int len)
 {
-	const void *p;
+	const char *p;
 
 	if (fdt_version(fdt) >= 0x11)
 		if (((offset + len) < offset)
libfdt/fdt_ro.c +2 -2
Index: dtc/libfdt/fdt_ro.c
===================================================================
@@ -408,10 +408,10 @@
 					     &phandle, sizeof(phandle));
 }
 
-int _stringlist_contains(const void *strlist, int listlen, const char *str)
+int _stringlist_contains(const char *strlist, int listlen, const char *str)
 {
 	int len = strlen(str);
-	const void *p;
+	const char *p;
 
 	while (listlen >= len) {
 		if (memcmp(str, strlist, len+1) == 0)
libfdt/libfdt_internal.h +3 -3
Index: dtc/libfdt/libfdt_internal.h
===================================================================
@@ -72,7 +72,7 @@
 
 static inline const void *_fdt_offset_ptr(const void *fdt, int offset)
 {
-	return fdt + fdt_off_dt_struct(fdt) + offset;
+	return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
 }
 
 static inline void *_fdt_offset_ptr_w(void *fdt, int offset)
@@ -82,8 +82,8 @@
 
 static inline const struct fdt_reserve_entry *_fdt_mem_rsv(const void *fdt, int n)
 {
-	const struct fdt_reserve_entry *rsv_table =
-		fdt + fdt_off_mem_rsvmap(fdt);
+	const struct fdt_reserve_entry *rsv_table = (struct fdt_reserve_entry *)
+		((const char *)fdt + fdt_off_mem_rsvmap(fdt));
 
 	return rsv_table + n;
 }
libfdt/fdt_sw.c +1 -1
Index: dtc/libfdt/fdt_sw.c
===================================================================
@@ -121,7 +121,7 @@
 	if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
 		return -FDT_ERR_NOSPACE;
 
-	re = (struct fdt_reserve_entry *)(fdt + offset);
+	re = (struct fdt_reserve_entry *)((char *)fdt + offset);
 	re->address = cpu_to_fdt64(addr);
 	re->size = cpu_to_fdt64(size);
 
libfdt/fdt_wip.c +1 -1
Index: dtc/libfdt/fdt_wip.c
===================================================================
@@ -76,7 +76,7 @@
 {
 	uint32_t *p;
 
-	for (p = start; (void *)p < (start + len); p++)
+	for (p = start; (char *)p < ((char *)start + len); p++)
 		*p = cpu_to_fdt32(FDT_NOP);
 }
 
libfdt/fdt_rw.c +28 -23

98 lines not on this page · raw message

ftdump.c +3 -3
Index: dtc/ftdump.c
===================================================================
@@ -70,9 +70,9 @@
 	uint32_t off_dt = fdt32_to_cpu(bph->off_dt_struct);
 	uint32_t off_str = fdt32_to_cpu(bph->off_dt_strings);
 	struct fdt_reserve_entry *p_rsvmap =
-		(struct fdt_reserve_entry *)(blob + off_mem_rsvmap);
-	char *p_struct = blob + off_dt;
-	char *p_strings = blob + off_str;
+		(struct fdt_reserve_entry *)((char *)blob + off_mem_rsvmap);
+	char *p_struct = (char *)blob + off_dt;
+	char *p_strings = (char *)blob + off_str;
 	uint32_t version = fdt32_to_cpu(bph->version);
 	uint32_t totalsize = fdt32_to_cpu(bph->totalsize);
 	uint32_t tag;
tests/incbin.c +4 -3
Index: dtc/tests/incbin.c
===================================================================
@@ -31,10 +31,10 @@
 
 #define CHUNKSIZE	1024
 
-void *load_file(const char *name, int *len)
+char *load_file(const char *name, int *len)
 {
 	FILE *f;
-	void *buf = NULL;
+	char *buf = NULL;
 	int bufsize = 0, n;
 
 	*len = 0;
@@ -60,7 +60,8 @@
 
 int main(int argc, char *argv[])
 {
-	void *fdt, *incbin;
+	void *fdt;
+	char *incbin;
 	int len;
 
 	test_init(argc, argv);
tests/mangle-layout.c +4 -4
Index: dtc/tests/mangle-layout.c
===================================================================
@@ -31,7 +31,7 @@
 #include "testdata.h"
 
 struct bufstate {
-	void *buf;
+	char *buf;
 	int size;
 };
 
@@ -73,7 +73,7 @@
 	case 'm':
 		/* Memory reserve map */
 		align = 8;
-		src = fdt + fdt_off_mem_rsvmap(fdt);
+		src = (const char *)fdt + fdt_off_mem_rsvmap(fdt);
 		size = (fdt_num_mem_rsv(fdt) + 1)
 			* sizeof(struct fdt_reserve_entry);
 		break;
@@ -81,14 +81,14 @@
 	case 't':
 		/* Structure block */
 		align = 4;
-		src = fdt + fdt_off_dt_struct(fdt);
+		src = (const char *)fdt + fdt_off_dt_struct(fdt);
 		size = fdt_size_dt_struct(fdt);
 		break;
 
 	case 's':
 		/* Strings block */
 		align = 1;
-		src = fdt + fdt_off_dt_strings(fdt);
+		src = (const char *)fdt + fdt_off_dt_strings(fdt);
 		size = fdt_size_dt_strings(fdt);
 		break;
 	default:
tests/move_and_save.c +1 -1
Index: dtc/tests/move_and_save.c
===================================================================
@@ -33,7 +33,7 @@
 int main(int argc, char *argv[])
 {
 	void *fdt, *fdt1, *fdt2, *fdt3;
-	void *buf;
+	char *buf;
 	int shuntsize;
 	int bufsize;
 	int err;
tests/nopulate.c +3 -4
Index: dtc/tests/nopulate.c
===================================================================
@@ -30,7 +30,7 @@
 #include "tests.h"
 #include "testdata.h"
 
-int nopulate_struct(char *buf, const void *fdt)
+int nopulate_struct(char *buf, const char *fdt)
 {
 	int offset, nextoffset = 0;
 	uint32_t tag;
@@ -42,7 +42,7 @@
 		offset = nextoffset;
 		tag = fdt_next_tag(fdt, offset, &nextoffset);
 
-		memcpy(p, fdt + fdt_off_dt_struct(fdt) + offset,
+		memcpy(p, (const char *)fdt + fdt_off_dt_struct(fdt) + offset,
 		       nextoffset - offset);
 		p += nextoffset - offset;
 
@@ -56,8 +56,7 @@
 
 int main(int argc, char *argv[])
 {
-	void *fdt, *fdt2;
-	void *buf;
+	char *fdt, *fdt2, *buf;
 	int newsize, struct_start, struct_end_old, struct_end_new, delta;
 	const char *inname;
 	char outname[PATH_MAX];
tests/testutils.c +1 -1
Index: dtc/tests/testutils.c
===================================================================
@@ -200,7 +200,7 @@
 	int fd;
 	int totalsize;
 	int offset;
-	void *p;
+	char *p;
 	int ret;
 
 	fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#1 lore raw
Quoted 6 lines

This patch turns on the -Wpointer-arith option in the dtc Makefile, and fixes the resulting warnings due to using (void *) in pointer arithmetic. While convenient, pointer arithmetic on void * is not portable, so it's better that we avoid it, particularly in libfdt.

This patch didn't apply. I tracked it down to bad context in the Makefile where this appeared:

Quoted 2 lines

CPPFLAGS += -std=c99 -D_XOPEN_SOURCE -D_BSD_SOURCE CFLAGS += -Werror

but the file had this instead:

Quoted 2 lines

BISON = bison
LEX = flex

in this part:

Makefile line 22

Quoted 13 lines

Index: dtc/Makefile
===================================================================

Makefile +1 -1
@@ -16,7 +16,7 @@
 CONFIG_LOCALVERSION =
 
 CPPFLAGS = -I libfdt
-CFLAGS = -Wall -g -Os
+CFLAGS = -Wall -g -Os -Wpointer-arith
 
 CPPFLAGS += -std=c99 -D_XOPEN_SOURCE -D_BSD_SOURCE
 CFLAGS += -Werror

Again, I hand whacked-the patch mail and applied it.

For the love-of-Pete, please use git.

jdl