2021-11-05 18:27:19

by Anders Roxell

[permalink] [raw]
Subject: [PATCH 1/2] selftests: timens: use 'llabs()' over 'abs()'

When building selftests/timens with clang, the compiler warn about the
function abs() see below:

timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]
if (abs(elapsed - 3600) > 60) {
^
timerfd.c:64:7: note: use function 'llabs' instead
if (abs(elapsed - 3600) > 60) {
^~~
llabs

The note indicates what to do, Rework to use the function 'llabs()'.

Signed-off-by: Anders Roxell <[email protected]>
---
tools/testing/selftests/timens/timer.c | 2 +-
tools/testing/selftests/timens/timerfd.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/timens/timer.c b/tools/testing/selftests/timens/timer.c
index 5e7f0051bd7b..5b939f59dfa4 100644
--- a/tools/testing/selftests/timens/timer.c
+++ b/tools/testing/selftests/timens/timer.c
@@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
return pr_perror("timerfd_gettime");

elapsed = new_value.it_value.tv_sec;
- if (abs(elapsed - 3600) > 60) {
+ if (llabs(elapsed - 3600) > 60) {
ksft_test_result_fail("clockid: %d elapsed: %lld\n",
clockid, elapsed);
return 1;
diff --git a/tools/testing/selftests/timens/timerfd.c b/tools/testing/selftests/timens/timerfd.c
index 9edd43d6b2c1..a4196bbd6e33 100644
--- a/tools/testing/selftests/timens/timerfd.c
+++ b/tools/testing/selftests/timens/timerfd.c
@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
return pr_perror("timerfd_gettime(%d)", clockid);

elapsed = new_value.it_value.tv_sec;
- if (abs(elapsed - 3600) > 60) {
+ if (llabs(elapsed - 3600) > 60) {
ksft_test_result_fail("clockid: %d elapsed: %lld\n",
clockid, elapsed);
return 1;
--
2.33.0


2021-11-05 23:58:59

by Nick Desaulniers

[permalink] [raw]
Subject: Re: [PATCH 1/2] selftests: timens: use 'llabs()' over 'abs()'

On Fri, Nov 5, 2021 at 9:31 AM Anders Roxell <[email protected]> wrote:
>
> When building selftests/timens with clang, the compiler warn about the
> function abs() see below:
>
> timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]
> if (abs(elapsed - 3600) > 60) {
> ^
> timerfd.c:64:7: note: use function 'llabs' instead
> if (abs(elapsed - 3600) > 60) {
> ^~~
> llabs
>
> The note indicates what to do, Rework to use the function 'llabs()'.
>
> Signed-off-by: Anders Roxell <[email protected]>

Thanks for the patch!
Reviewed-by: Nick Desaulniers <[email protected]>

> ---
> tools/testing/selftests/timens/timer.c | 2 +-
> tools/testing/selftests/timens/timerfd.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/tools/testing/selftests/timens/timer.c b/tools/testing/selftests/timens/timer.c
> index 5e7f0051bd7b..5b939f59dfa4 100644
> --- a/tools/testing/selftests/timens/timer.c
> +++ b/tools/testing/selftests/timens/timer.c
> @@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
> return pr_perror("timerfd_gettime");
>
> elapsed = new_value.it_value.tv_sec;
> - if (abs(elapsed - 3600) > 60) {
> + if (llabs(elapsed - 3600) > 60) {
> ksft_test_result_fail("clockid: %d elapsed: %lld\n",
> clockid, elapsed);
> return 1;
> diff --git a/tools/testing/selftests/timens/timerfd.c b/tools/testing/selftests/timens/timerfd.c
> index 9edd43d6b2c1..a4196bbd6e33 100644
> --- a/tools/testing/selftests/timens/timerfd.c
> +++ b/tools/testing/selftests/timens/timerfd.c
> @@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
> return pr_perror("timerfd_gettime(%d)", clockid);
>
> elapsed = new_value.it_value.tv_sec;
> - if (abs(elapsed - 3600) > 60) {
> + if (llabs(elapsed - 3600) > 60) {
> ksft_test_result_fail("clockid: %d elapsed: %lld\n",
> clockid, elapsed);
> return 1;
> --
> 2.33.0
>


--
Thanks,
~Nick Desaulniers

2021-11-20 00:18:45

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH 1/2] selftests: timens: use 'llabs()' over 'abs()'

On 11/5/21 2:26 PM, Nick Desaulniers wrote:
> On Fri, Nov 5, 2021 at 9:31 AM Anders Roxell <[email protected]> wrote:
>>
>> When building selftests/timens with clang, the compiler warn about the
>> function abs() see below:
>>
>> timerfd.c:64:7: error: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Werror,-Wabsolute-value]
>> if (abs(elapsed - 3600) > 60) {
>> ^
>> timerfd.c:64:7: note: use function 'llabs' instead
>> if (abs(elapsed - 3600) > 60) {
>> ^~~
>> llabs
>>
>> The note indicates what to do, Rework to use the function 'llabs()'.
>>
>> Signed-off-by: Anders Roxell <[email protected]>
>
> Thanks for the patch!
> Reviewed-by: Nick Desaulniers <[email protected]>
>
>> ---
>> tools/testing/selftests/timens/timer.c | 2 +-
>> tools/testing/selftests/timens/timerfd.c | 2 +-
>> 2 files changed, 2 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/testing/selftests/timens/timer.c b/tools/testing/selftests/timens/timer.c
>> index 5e7f0051bd7b..5b939f59dfa4 100644
>> --- a/tools/testing/selftests/timens/timer.c
>> +++ b/tools/testing/selftests/timens/timer.c
>> @@ -56,7 +56,7 @@ int run_test(int clockid, struct timespec now)
>> return pr_perror("timerfd_gettime");
>>
>> elapsed = new_value.it_value.tv_sec;
>> - if (abs(elapsed - 3600) > 60) {
>> + if (llabs(elapsed - 3600) > 60) {
>> ksft_test_result_fail("clockid: %d elapsed: %lld\n",
>> clockid, elapsed);
>> return 1;
>> diff --git a/tools/testing/selftests/timens/timerfd.c b/tools/testing/selftests/timens/timerfd.c
>> index 9edd43d6b2c1..a4196bbd6e33 100644
>> --- a/tools/testing/selftests/timens/timerfd.c
>> +++ b/tools/testing/selftests/timens/timerfd.c
>> @@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)
>> return pr_perror("timerfd_gettime(%d)", clockid);
>>
>> elapsed = new_value.it_value.tv_sec;
>> - if (abs(elapsed - 3600) > 60) {
>> + if (llabs(elapsed - 3600) > 60) {
>> ksft_test_result_fail("clockid: %d elapsed: %lld\n",
>> clockid, elapsed);
>> return 1;
>> --
>> 2.33.0
>>
>
>

Same comment on llabs() define in stdlib.h made earlier in the context
of timer test changes.

thanks,
-- Shuah