[PATCH v2 3/7] selftests/nolibc: fix up compile warning with glibc on x86_64

Willy Tarreau w at 1wt.eu
Mon May 29 06:04:49 PDT 2023


On Mon, May 29, 2023 at 09:00:01PM +0800, Zhangjin Wu wrote:
> Compiling nolibc-test.c with gcc on x86_64 got such warning:
> 
> tools/testing/selftests/nolibc/nolibc-test.c: In function 'expect_eq':
> tools/testing/selftests/nolibc/nolibc-test.c:177:24: warning: format '%lld' expects argument of type 'long long int', but argument 2 has type 'uint64_t' {aka 'long unsigned int'} [-Wformat=]
>   177 |  llen += printf(" = %lld ", expr);
>       |                     ~~~^    ~~~~
>       |                        |    |
>       |                        |    uint64_t {aka long unsigned int}
>       |                        long long int
>       |                     %ld
> 
> It because that glibc defines uint64_t as "unsigned long int" when word
> size (means sizeof(long)) is 64bit (see include/bits/types.h), but
> nolibc directly use the 64bit "unsigned long long" (see
> tools/include/nolibc/stdint.h), which is simpler, seems kernel uses it
> too (include/uapi/asm-generic/int-ll64.h).
> 
> It is able to do like glibc, defining __WORDSIZE for all of platforms
> and using "unsigned long int" to define uint64_t when __WORDSIZE is
> 64bits, but here uses a simpler solution: nolibc always requires %lld to
> match "unsigned long long", for others, only require %lld when word size
> is 32bit.
> 
> Signed-off-by: Zhangjin Wu <falcon at tinylab.org>
> ---
>  tools/testing/selftests/nolibc/nolibc-test.c | 4 ++++
>  1 file changed, 4 insertions(+)
> 
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index d417ca5d976f..7f9b716fd9b1 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -174,7 +174,11 @@ static int expect_eq(uint64_t expr, int llen, uint64_t val)
>  {
>  	int ret = !(expr == val);
>  
> +#if __SIZEOF_LONG__ == 4 || defined(NOLIBC)
>  	llen += printf(" = %lld ", expr);
> +#else
> +	llen += printf(" = %ld ", expr);
> +#endif
>  	pad_spc(llen, 64, ret ? "[FAIL]\n" : " [OK]\n");
>  	return ret;
>  }

Please don't proceed like this. There's much easier to do here for a printf,
just cast the expression to the type printf expects:

-  	llen += printf(" = %lld ", expr);
+  	llen += printf(" = %lld ", (long long)expr);

Willy



More information about the linux-riscv mailing list