diff mbox

[v2] Fix remaining compiler warnings in sloffs.c

Message ID 1470688356-31810-1-git-send-email-thuth@redhat.com
State Accepted
Headers show

Commit Message

Thomas Huth Aug. 8, 2016, 8:32 p.m. UTC
With my version of GCC (v4.8.5 - Advance-Toolchain 7.0) there are
currently two warnings when compiling sloffs.c:

sloffs.c: In function 'sloffs_dump':
sloffs.c:437:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
      printf("%04x", be16_to_cpu(*(uint16_t *)(header->date + 2)));
      ^
sloffs.c:449:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
      printf("%04x", be16_to_cpu(*(uint16_t *)(header->mdate + 2)));
      ^

These can be easily fixed by accessing the memory byte by byte instead of
casting the pointer to (uint16_t *). And while we're at it, let's also
simplify the code a little bit by consolidating the date print code
into a separate function which can be used by the two spots that print
a date.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 tools/sloffs.c | 36 +++++++++++++++---------------------
 1 file changed, 15 insertions(+), 21 deletions(-)

Comments

Alexey Kardashevskiy Aug. 16, 2016, 9:13 a.m. UTC | #1
On 09/08/16 06:32, Thomas Huth wrote:
> With my version of GCC (v4.8.5 - Advance-Toolchain 7.0) there are
> currently two warnings when compiling sloffs.c:
> 
> sloffs.c: In function 'sloffs_dump':
> sloffs.c:437:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>       printf("%04x", be16_to_cpu(*(uint16_t *)(header->date + 2)));
>       ^
> sloffs.c:449:6: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>       printf("%04x", be16_to_cpu(*(uint16_t *)(header->mdate + 2)));
>       ^
> 
> These can be easily fixed by accessing the memory byte by byte instead of
> casting the pointer to (uint16_t *). And while we're at it, let's also
> simplify the code a little bit by consolidating the date print code
> into a separate function which can be used by the two spots that print
> a date.
> 
> Signed-off-by: Thomas Huth <thuth@redhat.com>
> ---


Thanks, applied.


>  tools/sloffs.c | 36 +++++++++++++++---------------------
>  1 file changed, 15 insertions(+), 21 deletions(-)
> 
> diff --git a/tools/sloffs.c b/tools/sloffs.c
> index 91a23c3..9a1eace 100644
> --- a/tools/sloffs.c
> +++ b/tools/sloffs.c
> @@ -405,6 +405,19 @@ sloffs_append(const int file, const char *name, const char *dest)
>  	close(out);
>  }
>  
> +static void print_header_date(void *dptr)
> +{
> +	uint8_t *date = dptr;
> +
> +	if (date[2] || date[3] || date[4] || date[5] || date[6] || date[7]) {
> +		printf("%02x%02x-%02x-%02x %02x:%02x", date[2], date[3],
> +		       date[4], date[5], date[6], date[7]);
> +	} else {
> +		printf("N/A");
> +	}
> +
> +}
> +
>  static void
>  sloffs_dump(const int fd)
>  {
> @@ -413,7 +426,6 @@ sloffs_dump(const int fd)
>  	struct sloffs file;
>  	int i;
>  	uint64_t crc;
> -	uint64_t *datetmp;
>  	uint64_t header_len;
>  
>  	header = sloffs_header(fd);
> @@ -432,28 +444,10 @@ sloffs_dump(const int fd)
>  	/* there is a bug in the date position;
>  	 * it should be at header->date, but it is at (header->date + 2) */
>  	printf("  Build Date  : ");
> -	datetmp = (void *)header->date;
> -	if (be64_to_cpu(*datetmp)) {
> -	    printf("%04x", be16_to_cpu(*(uint16_t *)(header->date + 2)));
> -	    printf("-%02x", *(uint8_t *)(header->date + 4));
> -	    printf("-%02x", *(uint8_t *)(header->date + 5));
> -	    printf(" %02x:", *(uint8_t *)(header->date + 6));
> -	    printf("%02x", *(uint8_t *)(header->date + 7));
> -	} else {
> -	    printf("N/A");
> -	}
> +	print_header_date(header->date);
>  	printf("\n");
>  	printf("  Modify Date : ");
> -	datetmp = (void *)header->mdate;
> -	if (be64_to_cpu(*datetmp)) {
> -	    printf("%04x", be16_to_cpu(*(uint16_t *)(header->mdate + 2)));
> -	    printf("-%02x", *(uint8_t *)(header->mdate + 4));
> -	    printf("-%02x", *(uint8_t *)(header->mdate + 5));
> -	    printf(" %02x:", *(uint8_t *)(header->mdate + 6));
> -	    printf("%02x", *(uint8_t *)(header->mdate + 7));
> -	} else {
> -	    printf("N/A");
> -	}
> +	print_header_date(header->mdate);
>  	printf("\n");
>  	printf("  Image Length: %ld", be64_to_cpu(header->flashlen));
>  	printf(" (0x%lx) bytes\n", be64_to_cpu(header->flashlen));
>
diff mbox

Patch

diff --git a/tools/sloffs.c b/tools/sloffs.c
index 91a23c3..9a1eace 100644
--- a/tools/sloffs.c
+++ b/tools/sloffs.c
@@ -405,6 +405,19 @@  sloffs_append(const int file, const char *name, const char *dest)
 	close(out);
 }
 
+static void print_header_date(void *dptr)
+{
+	uint8_t *date = dptr;
+
+	if (date[2] || date[3] || date[4] || date[5] || date[6] || date[7]) {
+		printf("%02x%02x-%02x-%02x %02x:%02x", date[2], date[3],
+		       date[4], date[5], date[6], date[7]);
+	} else {
+		printf("N/A");
+	}
+
+}
+
 static void
 sloffs_dump(const int fd)
 {
@@ -413,7 +426,6 @@  sloffs_dump(const int fd)
 	struct sloffs file;
 	int i;
 	uint64_t crc;
-	uint64_t *datetmp;
 	uint64_t header_len;
 
 	header = sloffs_header(fd);
@@ -432,28 +444,10 @@  sloffs_dump(const int fd)
 	/* there is a bug in the date position;
 	 * it should be at header->date, but it is at (header->date + 2) */
 	printf("  Build Date  : ");
-	datetmp = (void *)header->date;
-	if (be64_to_cpu(*datetmp)) {
-	    printf("%04x", be16_to_cpu(*(uint16_t *)(header->date + 2)));
-	    printf("-%02x", *(uint8_t *)(header->date + 4));
-	    printf("-%02x", *(uint8_t *)(header->date + 5));
-	    printf(" %02x:", *(uint8_t *)(header->date + 6));
-	    printf("%02x", *(uint8_t *)(header->date + 7));
-	} else {
-	    printf("N/A");
-	}
+	print_header_date(header->date);
 	printf("\n");
 	printf("  Modify Date : ");
-	datetmp = (void *)header->mdate;
-	if (be64_to_cpu(*datetmp)) {
-	    printf("%04x", be16_to_cpu(*(uint16_t *)(header->mdate + 2)));
-	    printf("-%02x", *(uint8_t *)(header->mdate + 4));
-	    printf("-%02x", *(uint8_t *)(header->mdate + 5));
-	    printf(" %02x:", *(uint8_t *)(header->mdate + 6));
-	    printf("%02x", *(uint8_t *)(header->mdate + 7));
-	} else {
-	    printf("N/A");
-	}
+	print_header_date(header->mdate);
 	printf("\n");
 	printf("  Image Length: %ld", be64_to_cpu(header->flashlen));
 	printf(" (0x%lx) bytes\n", be64_to_cpu(header->flashlen));