diff mbox

[U-Boot,10/17] tools: kwbimage: Fix arithmetic with void pointers

Message ID e240378834bf8d7f13bf4665bf558ba1f7a3be8a.1479913469.git.mario.six@gdsys.cc
State Accepted
Commit 885fba155c70871cfbe83f13ed98cd804a049058
Delegated to: Stefan Roese
Headers show

Commit Message

Mario Six Nov. 23, 2016, 3:12 p.m. UTC
Arithmetic with void pointers, e.g. a - b where both a and b are void
pointers, is undefined in the C standard. Since we are operating with
byte data here, we switch the void pointers to uint8_t pointers, and add
the necessary casts.

Signed-off-by: Mario Six <mario.six@gdsys.cc>
---
 tools/kwbimage.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)
diff mbox

Patch

diff --git a/tools/kwbimage.c b/tools/kwbimage.c
index 0872097..6a110fc 100644
--- a/tools/kwbimage.c
+++ b/tools/kwbimage.c
@@ -205,7 +205,7 @@  static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 	size_t headersz;
 	struct main_hdr_v0 *main_hdr;
 	struct ext_hdr_v0 *ext_hdr;
-	void *image;
+	uint8_t *image;
 	int has_ext = 0;
 
 	/*
@@ -232,7 +232,7 @@  static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 
 	memset(image, 0, headersz);
 
-	main_hdr = image;
+	main_hdr = (struct main_hdr_v0 *)image;
 
 	/* Fill in the main header */
 	main_hdr->blocksize =
@@ -258,7 +258,8 @@  static void *image_create_v0(size_t *imagesz, struct image_tool_params *params,
 	if (has_ext) {
 		int cfgi, datai;
 
-		ext_hdr = image + sizeof(struct main_hdr_v0);
+		ext_hdr = (struct ext_hdr_v0 *)
+				(image + sizeof(struct main_hdr_v0));
 		ext_hdr->offset = cpu_to_le32(0x40);
 
 		for (cfgi = 0, datai = 0; cfgi < cfgn; cfgi++) {
@@ -359,7 +360,7 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 	struct image_cfg_element *e, *binarye;
 	struct main_hdr_v1 *main_hdr;
 	size_t headersz;
-	void *image, *cur;
+	uint8_t *image, *cur;
 	int hasext = 0;
 	int ret;
 
@@ -379,8 +380,8 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 
 	memset(image, 0, headersz);
 
-	cur = main_hdr = image;
-	cur += sizeof(struct main_hdr_v1);
+	main_hdr = (struct main_hdr_v1 *)image;
+	cur = image + sizeof(struct main_hdr_v1);
 
 	/* Fill the main header */
 	main_hdr->blocksize    =
@@ -405,7 +406,7 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 
 	binarye = image_find_option(IMAGE_CFG_BINARY);
 	if (binarye) {
-		struct opt_hdr_v1 *hdr = cur;
+		struct opt_hdr_v1 *hdr = (struct opt_hdr_v1 *)cur;
 		uint32_t *args;
 		size_t binhdrsz;
 		struct stat s;
@@ -438,7 +439,7 @@  static void *image_create_v1(size_t *imagesz, struct image_tool_params *params,
 
 		cur += sizeof(struct opt_hdr_v1);
 
-		args = cur;
+		args = (uint32_t *)cur;
 		*args = cpu_to_le32(binarye->binary.nargs);
 		args++;
 		for (argi = 0; argi < binarye->binary.nargs; argi++)
@@ -780,7 +781,7 @@  static int kwbimage_verify_header(unsigned char *ptr, int image_size,
 	struct ext_hdr_v0 *ext_hdr;
 	uint8_t checksum;
 
-	main_hdr = (void *)ptr;
+	main_hdr = (struct main_hdr_v0 *)ptr;
 	checksum = image_checksum8(ptr,
 				   sizeof(struct main_hdr_v0)
 				   - sizeof(uint8_t));
@@ -789,7 +790,8 @@  static int kwbimage_verify_header(unsigned char *ptr, int image_size,
 
 	/* Only version 0 extended header has checksum */
 	if (image_version((void *)ptr) == 0) {
-		ext_hdr = (void *)ptr + sizeof(struct main_hdr_v0);
+		ext_hdr = (struct ext_hdr_v0 *)
+				(ptr + sizeof(struct main_hdr_v0));
 		checksum = image_checksum8(ext_hdr,
 					   sizeof(struct ext_hdr_v0)
 					   - sizeof(uint8_t));