Re: [PATCH] powerpc: replace ternary operator with min()

From: Russell Currey
Date: Mon Oct 24 2022 - 00:33:42 EST


On Sun, 2022-10-23 at 20:44 +0800, KaiLong Wang wrote:
> Fix the following coccicheck warning:
>
> arch/powerpc/xmon/xmon.c:2987: WARNING opportunity for min()
> arch/powerpc/xmon/xmon.c:2583: WARNING opportunity for min()
>
> Signed-off-by: KaiLong Wang <wangkailong@xxxxxxx>

Hello,

This fails to compile on some platforms/compilers since n is a long and
16 is an int, expanding to:

r = __builtin_choose_expr(
((!!(sizeof((typeof(n) *)1 == (typeof(16) *)1))) &&
((sizeof(int) ==
sizeof(*(8 ? ((void *)((long)(n)*0l)) : (int *)8))) &&
(sizeof(int) ==
sizeof(*(8 ? ((void *)((long)(16) * 0l)) :
(int *)8))))),
((n) < (16) ? (n) : (16)), ({
typeof(n) __UNIQUE_ID___x0 = (n);
typeof(16) __UNIQUE_ID___y1 = (16);
((__UNIQUE_ID___x0) < (__UNIQUE_ID___y1) ?
(__UNIQUE_ID___x0) :
(__UNIQUE_ID___y1));
}));

Here's the full build failure as found by snowpatch:
https://github.com/ruscur/linux-ci/actions/runs/3308880562/jobs/5461579048#step:4:89

You should use min_t(long, n, 16) instead.

- Russell

> ---
>  arch/powerpc/xmon/xmon.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/arch/powerpc/xmon/xmon.c b/arch/powerpc/xmon/xmon.c
> index f51c882bf902..a7751cd2cc9d 100644
> --- a/arch/powerpc/xmon/xmon.c
> +++ b/arch/powerpc/xmon/xmon.c
> @@ -2580,7 +2580,7 @@ static void xmon_rawdump (unsigned long adrs,
> long ndump)
>         unsigned char temp[16];
>  
>         for (n = ndump; n > 0;) {
> -               r = n < 16? n: 16;
> +               r = min(n, 16);
>                 nr = mread(adrs, temp, r);
>                 adrs += nr;
>                 for (m = 0; m < r; ++m) {
> @@ -2984,7 +2984,7 @@ prdump(unsigned long adrs, long ndump)
>         for (n = ndump; n > 0;) {
>                 printf(REG, adrs);
>                 putchar(' ');
> -               r = n < 16? n: 16;
> +               r = min(n, 16);
>                 nr = mread(adrs, temp, r);
>                 adrs += nr;
>                 for (m = 0; m < r; ++m) {