Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1592774 > unrolled thread

[PATCH v4 1/1] x86, relocs: add printf attribute to die()

Started byNicolas Iooss <nicolas.iooss_linux@m4x.org>
First post2017-03-05 15:40 +0100
Last post2017-03-07 20:20 +0100
Articles 3 — 2 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v4 1/1] x86, relocs: add printf attribute to die() Nicolas Iooss <nicolas.iooss_linux@m4x.org> - 2017-03-05 15:40 +0100
    Re: [PATCH v4 1/1] x86, relocs: add printf attribute to die() Ingo Molnar <mingo@kernel.org> - 2017-03-07 14:30 +0100
      Re: [PATCH v4 1/1] x86, relocs: add printf attribute to die() Nicolas Iooss <nicolas.iooss_linux@m4x.org> - 2017-03-07 20:20 +0100

#1592774 — [PATCH v4 1/1] x86, relocs: add printf attribute to die()

FromNicolas Iooss <nicolas.iooss_linux@m4x.org>
Date2017-03-05 15:40 +0100
Subject[PATCH v4 1/1] x86, relocs: add printf attribute to die()
Message-ID<thFuh-1BG-5@gated-at.bofh.it>
Adding such an attribute helps to detect errors in the format string at
build time. After doing this, the compiler complains about some issues:

    arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Xword' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_size);
                                    ^~~~~~~~~~~~~~~~~
    arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
    but the argument has type 'Elf64_Off' (aka 'unsigned long')
    [-Werror,-Wformat]
                                    sec->shdr.sh_offset, strerror(errno));
                                    ^~~~~~~~~~~~~~~~~~~

When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
included by relocs_64.c, these expressions are 64-bit unsigned integers.

Add casts to unsigned long long, which length is always 64-bit, and use
%llu to format sec->shdr.sh_size and sec->shdr.sh_offset in relocs.c.

While at it, constify the format attribute of die().

Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
---
 arch/x86/tools/relocs.c        | 31 +++++++++++++++++--------------
 arch/x86/tools/relocs.h        |  3 ++-
 arch/x86/tools/relocs_common.c |  2 +-
 3 files changed, 20 insertions(+), 16 deletions(-)

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index 73eb7fd4aec4..3cc02065c677 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -397,8 +397,8 @@ static void read_shdrs(FILE *fp)
 		    ehdr.e_shnum);
 	}
 	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
-		die("Seek to %d failed: %s\n",
-			ehdr.e_shoff, strerror(errno));
+		die("Seek to %llu failed: %s\n",
+			(unsigned long long)ehdr.e_shoff, strerror(errno));
 	}
 	for (i = 0; i < ehdr.e_shnum; i++) {
 		struct section *sec = &secs[i];
@@ -431,12 +431,13 @@ static void read_strtabs(FILE *fp)
 		}
 		sec->strtab = malloc(sec->shdr.sh_size);
 		if (!sec->strtab) {
-			die("malloc of %d bytes for strtab failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %llu bytes for strtab failed\n",
+				(unsigned long long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %llu failed: %s\n",
+				(unsigned long long)sec->shdr.sh_offset,
+				strerror(errno));
 		}
 		if (fread(sec->strtab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
@@ -456,12 +457,13 @@ static void read_symtabs(FILE *fp)
 		}
 		sec->symtab = malloc(sec->shdr.sh_size);
 		if (!sec->symtab) {
-			die("malloc of %d bytes for symtab failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %llu bytes for symtab failed\n",
+				(unsigned long long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %llu failed: %s\n",
+				(unsigned long long)sec->shdr.sh_offset,
+				strerror(errno));
 		}
 		if (fread(sec->symtab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
@@ -489,12 +491,13 @@ static void read_relocs(FILE *fp)
 		}
 		sec->reltab = malloc(sec->shdr.sh_size);
 		if (!sec->reltab) {
-			die("malloc of %d bytes for relocs failed\n",
-				sec->shdr.sh_size);
+			die("malloc of %llu bytes for relocs failed\n",
+				(unsigned long long)sec->shdr.sh_size);
 		}
 		if (fseek(fp, sec->shdr.sh_offset, SEEK_SET) < 0) {
-			die("Seek to %d failed: %s\n",
-				sec->shdr.sh_offset, strerror(errno));
+			die("Seek to %llu failed: %s\n",
+				(unsigned long long)sec->shdr.sh_offset,
+				strerror(errno));
 		}
 		if (fread(sec->reltab, 1, sec->shdr.sh_size, fp)
 		    != sec->shdr.sh_size) {
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 1d23bf953a4a..f41416c31608 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -16,7 +16,8 @@
 #include <regex.h>
 #include <tools/le_byteshift.h>
 
-void die(char *fmt, ...) __attribute__((noreturn));
+void die(const char *fmt, ...) __attribute__((noreturn))
+	__attribute__((format(printf, 1, 2)));
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
index acab636bcb34..30adb44eff79 100644
--- a/arch/x86/tools/relocs_common.c
+++ b/arch/x86/tools/relocs_common.c
@@ -1,6 +1,6 @@
 #include "relocs.h"
 
-void die(char *fmt, ...)
+void die(const char *fmt, ...)
 {
 	va_list ap;
 	va_start(ap, fmt);
-- 
2.11.1

[toc] | [next] | [standalone]


#1594238

FromIngo Molnar <mingo@kernel.org>
Date2017-03-07 14:30 +0100
Message-ID<tinlE-83G-13@gated-at.bofh.it>
In reply to#1592774
* Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:

> Adding such an attribute helps to detect errors in the format string at
> build time. After doing this, the compiler complains about some issues:
> 
>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_size);
>                                     ^~~~~~~~~~~~~~~~~
>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>     [-Werror,-Wformat]
>                                     sec->shdr.sh_offset, strerror(errno));
>                                     ^~~~~~~~~~~~~~~~~~~
> 
> When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
> sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
> included by relocs_64.c, these expressions are 64-bit unsigned integers.
> 
> Add casts to unsigned long long, which length is always 64-bit, and use
> %llu to format sec->shdr.sh_size and sec->shdr.sh_offset in relocs.c.
> 
> While at it, constify the format attribute of die().
> 
> Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
> ---
>  arch/x86/tools/relocs.c        | 31 +++++++++++++++++--------------
>  arch/x86/tools/relocs.h        |  3 ++-
>  arch/x86/tools/relocs_common.c |  2 +-
>  3 files changed, 20 insertions(+), 16 deletions(-)
> 
> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
> index 73eb7fd4aec4..3cc02065c677 100644
> --- a/arch/x86/tools/relocs.c
> +++ b/arch/x86/tools/relocs.c
> @@ -397,8 +397,8 @@ static void read_shdrs(FILE *fp)
>  		    ehdr.e_shnum);
>  	}
>  	if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
> -		die("Seek to %d failed: %s\n",
> -			ehdr.e_shoff, strerror(errno));
> +		die("Seek to %llu failed: %s\n",
> +			(unsigned long long)ehdr.e_shoff, strerror(errno));

Isn't "(u64)" shorter to write?

Thanks,

	Ingo

[toc] | [prev] | [next] | [standalone]


#1594561

FromNicolas Iooss <nicolas.iooss_linux@m4x.org>
Date2017-03-07 20:20 +0100
Message-ID<tisOm-3ze-25@gated-at.bofh.it>
In reply to#1594238
On Tue, Mar 7, 2017 at 10:16 AM, Ingo Molnar <mingo@kernel.org> wrote:
>
> * Nicolas Iooss <nicolas.iooss_linux@m4x.org> wrote:
>
>> Adding such an attribute helps to detect errors in the format string at
>> build time. After doing this, the compiler complains about some issues:
>>
>>     arch/x86/tools/relocs.c:460:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Xword' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_size);
>>                                     ^~~~~~~~~~~~~~~~~
>>     arch/x86/tools/relocs.c:464:5: error: format specifies type 'int'
>>     but the argument has type 'Elf64_Off' (aka 'unsigned long')
>>     [-Werror,-Wformat]
>>                                     sec->shdr.sh_offset, strerror(errno));
>>                                     ^~~~~~~~~~~~~~~~~~~
>>
>> When relocs.c is included by relocs_32.c, sec->shdr.sh_size and
>> sec->shdr.sh_offset are 32-bit unsigned integers. When the file is
>> included by relocs_64.c, these expressions are 64-bit unsigned integers.
>>
>> Add casts to unsigned long long, which length is always 64-bit, and use
>> %llu to format sec->shdr.sh_size and sec->shdr.sh_offset in relocs.c.
>>
>> While at it, constify the format attribute of die().
>>
>> Signed-off-by: Nicolas Iooss <nicolas.iooss_linux@m4x.org>
>> ---
>>  arch/x86/tools/relocs.c        | 31 +++++++++++++++++--------------
>>  arch/x86/tools/relocs.h        |  3 ++-
>>  arch/x86/tools/relocs_common.c |  2 +-
>>  3 files changed, 20 insertions(+), 16 deletions(-)
>>
>> diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
>> index 73eb7fd4aec4..3cc02065c677 100644
>> --- a/arch/x86/tools/relocs.c
>> +++ b/arch/x86/tools/relocs.c
>> @@ -397,8 +397,8 @@ static void read_shdrs(FILE *fp)
>>                   ehdr.e_shnum);
>>       }
>>       if (fseek(fp, ehdr.e_shoff, SEEK_SET) < 0) {
>> -             die("Seek to %d failed: %s\n",
>> -                     ehdr.e_shoff, strerror(errno));
>> +             die("Seek to %llu failed: %s\n",
>> +                     (unsigned long long)ehdr.e_shoff, strerror(errno));
>
> Isn't "(u64)" shorter to write?

u64 does not seem to be defined in this file right now. Adding
"#include <linux/types.h>" defines u64 and __u64 in the following way:
- "typedef uint64_t u64;" from tools/include/linux/types.h
- "typedef unsigned long long __u64;" from /usr/include/asm-generic/int-ll64.h

uint64_t is unsigned long on x86-64 and gcc complains when using %llu
on such a type, so using a cast to u64 forces using PRIu64 too.

Nevertheless "(__u64)" is shorter than "(unsigned long long)" and
seems to work fine in my quick tests because it is always unsigned
long long (on both x86-32 and x86-64). Would you prefer to use this
cast?

Thanks,
Nicolas

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web