All of lore.kernel.org
 help / color / mirror / Atom feed
From: Nick Desaulniers <ndesaulniers@google.com>
To: Anders Roxell <anders.roxell@linaro.org>
Cc: shuah@kernel.org, nathan@kernel.org,
	linux-kselftest@vger.kernel.org,  linux-kernel@vger.kernel.org,
	llvm@lists.linux.dev,  Arnd Bergmann <arnd@arndb.de>
Subject: Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'
Date: Wed, 10 Nov 2021 12:09:40 -0800	[thread overview]
Message-ID: <CAKwvOdkoKvjecTfxbRwZ=BYirvy-Jq64pHtHuct8oWgExv1xPw@mail.gmail.com> (raw)
In-Reply-To: <20211110180359.2338349-1-anders.roxell@linaro.org>

On Wed, Nov 10, 2021 at 10:04 AM Anders Roxell <anders.roxell@linaro.org> wrote:
>
> When building selftests/timens with clang, the compiler warn about the
> function abs() see below:
>
> exec.c:33:8: error: absolute value function 'abs' given an argument of type 'long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]
>                         if (abs(tst.tv_sec - now.tv_sec) > 5)
>                             ^
> exec.c:33:8: note: use function 'labs' instead
>                         if (abs(tst.tv_sec - now.tv_sec) > 5)
>                             ^~~
>                             labs
>
> Rework to store the time difference in a 'long long' and pass that to
> llabs(), since the variable can be an 'int', 'long' or 'long long'
> depending on the architecture and C library.
>
> Suggested-by: Arnd Bergmann <arnd@arndb.de>
> Signed-off-by: Anders Roxell <anders.roxell@linaro.org>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>

> ---
>  tools/testing/selftests/timens/exec.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
>
> diff --git a/tools/testing/selftests/timens/exec.c b/tools/testing/selftests/timens/exec.c
> index e40dc5be2f66..04439e6ac8a2 100644
> --- a/tools/testing/selftests/timens/exec.c
> +++ b/tools/testing/selftests/timens/exec.c
> @@ -21,6 +21,7 @@
>  int main(int argc, char *argv[])
>  {
>         struct timespec now, tst;
> +       long long timediff;
>         int status, i;
>         pid_t pid;
>
> @@ -30,7 +31,8 @@ int main(int argc, char *argv[])
>
>                 for (i = 0; i < 2; i++) {
>                         _gettime(CLOCK_MONOTONIC, &tst, i);
> -                       if (abs(tst.tv_sec - now.tv_sec) > 5)
> +                       timediff = tst.tv_sec - now.tv_sec;
> +                       if (llabs(timediff) > 5)
>                                 return pr_fail("%ld %ld\n", now.tv_sec, tst.tv_sec);
>                 }
>                 return 0;
> @@ -50,7 +52,8 @@ int main(int argc, char *argv[])
>
>         for (i = 0; i < 2; i++) {
>                 _gettime(CLOCK_MONOTONIC, &tst, i);
> -               if (abs(tst.tv_sec - now.tv_sec) > 5)
> +               timediff = tst.tv_sec - now.tv_sec;
> +               if (llabs(timediff) > 5)
>                         return pr_fail("%ld %ld\n",
>                                         now.tv_sec, tst.tv_sec);
>         }
> @@ -70,7 +73,8 @@ int main(int argc, char *argv[])
>                 /* Check that a child process is in the new timens. */
>                 for (i = 0; i < 2; i++) {
>                         _gettime(CLOCK_MONOTONIC, &tst, i);
> -                       if (abs(tst.tv_sec - now.tv_sec - OFFSET) > 5)
> +                       timediff = tst.tv_sec - now.tv_sec - OFFSET;
> +                       if (llabs(timediff) > 5)
>                                 return pr_fail("%ld %ld\n",
>                                                 now.tv_sec + OFFSET, tst.tv_sec);
>                 }
> --
> 2.33.0
>


-- 
Thanks,
~Nick Desaulniers

  reply	other threads:[~2021-11-10 20:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-05 16:31 [PATCH 1/2] selftests: timens: use 'llabs()' over 'abs()' Anders Roxell
2021-11-05 16:31 ` [PATCH 2/2] selftests: timens: exec: use 'labs()' " Anders Roxell
2021-11-05 20:35   ` Nick Desaulniers
2021-11-05 20:45     ` Arnd Bergmann
2021-11-05 20:50       ` Nick Desaulniers
2021-11-10 18:03       ` [PATCHv2] " Anders Roxell
2021-11-10 20:09         ` Nick Desaulniers [this message]
2021-11-10 20:11           ` Nick Desaulniers
2021-11-11  8:26             ` Anders Roxell
2021-11-05 20:26 ` [PATCH 1/2] selftests: timens: use 'llabs()' " Nick Desaulniers
2021-11-20  0:18   ` Shuah Khan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAKwvOdkoKvjecTfxbRwZ=BYirvy-Jq64pHtHuct8oWgExv1xPw@mail.gmail.com' \
    --to=ndesaulniers@google.com \
    --cc=anders.roxell@linaro.org \
    --cc=arnd@arndb.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=llvm@lists.linux.dev \
    --cc=nathan@kernel.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.