[SLOF] [PATCH] Fix remaining compiler warnings in sloffs.c

Adrian Reber areber at redhat.com
Fri Aug 5 18:48:18 AEST 2016


On Wed, Aug 03, 2016 at 10:37:48PM +0200, 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 at redhat.com>
> ---

I do not see this warning with gcc 6.1.1 but I also see it with 4.8.5
and with this patch it is fixed.

Reviewed-by: Adrian Reber <areber at redhat.com>


> diff --git a/tools/sloffs.c b/tools/sloffs.c
> index 91a23c3..764c956 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 (*(uint64_t *)dptr != 0) {
> +		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));
> -- 
> 1.8.3.1
> 


More information about the SLOF mailing list