Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'

From: Nick Desaulniers
Date: Wed Nov 10 2021 - 15:11:28 EST


On Wed, Nov 10, 2021 at 12:09 PM Nick Desaulniers
<ndesaulniers@xxxxxxxxxx> wrote:
>
> On Wed, Nov 10, 2021 at 10:04 AM Anders Roxell <anders.roxell@xxxxxxxxxx> 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@xxxxxxxx>
> > Signed-off-by: Anders Roxell <anders.roxell@xxxxxxxxxx>
>
> Thanks for the patch!
> Reviewed-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>

ah, gmail doesn't do a great job at showing the subject when a v2 is
sent in-reply-to. Should the oneline mention llabs rather than labs
now?

>
> > ---
> > 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



--
Thanks,
~Nick Desaulniers