selftests: timens: use 'llabs()' over 'abs()'
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) {
^~~
llabsThe note indicates what to do, Rework to use the function 'llabs()'.
--- tools/testing/selftests/timens/timer.c | 2 +- tools/testing/selftests/timens/timerfd.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
tools/testing/selftests/timens/timer.c +1 -1
@@ -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 ;
tools/testing/selftests/timens/timerfd.c +1 -1
@@ -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 ;
Signature · 1 line
2.33.0
PATCH 2/2 selftests: timens: exec: use 'labs()' over 'abs()'
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)
^~~
labsThe note indicates what to do, Rework to use the function 'labs()'.
--- tools/testing/selftests/timens/exec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +3 -3
@@ -30,7 +30,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec, tst.tv_sec); }return 0 ;
@@ -50,7 +50,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec, tst.tv_sec); }
@@ -70,7 +70,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec -OFFSET ) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec +OFFSET , tst.tv_sec); }
Signature · 1 line
2.33.0
Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 11 lines · Anders Roxell
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
Careful.
Isn't the tv_sec member of struct timespec a time_t which is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.
So I'm in favor of this patch (dispatching to abs or labs based on 64b host) would hurt readability.
Quoted 42 lines
The note indicates what to do, Rework to use the function '
labs()'.Signed-off-by Anders Roxell <[email protected]>--- tools/testing/selftests/timens/exec.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)tools/testing/selftests/timens/exec.c +3
-4@@ -30,7 +30,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec, tst.tv_sec); }return 0 ;@@ -50,7 +50,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec, tst.tv_sec); }@@ -70,7 +70,7 @@ 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 ) +if (labs (tst.tv_sec - now.tv_sec -OFFSET ) >5 )return pr_fail ("%ld %ld\n" , now.tv_sec +OFFSET , tst.tv_sec); } --2.33.0
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 14 lines · Nick Desaulniers
Quoted 11 lines · Anders Roxell
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
Quoted 7 lines
Careful.
Isn't the
tv_secmember ofstruct timespecatime_twhich is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.
It could actually be 'int', 'long' or 'long long' depending on the architecture and C library. Maybe we need a temporary variable of type 'long long' to hold the difference, and pass that to llabs()?
Arnd
Re: [PATCH 2/2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 24 lines · Arnd Bergmann
Quoted 14 lines · Nick Desaulniers
Quoted 11 lines · Anders Roxell
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) ^~~ labsQuoted 7 lines
Careful.
Isn't the
tv_secmember ofstruct timespecatime_twhich is 32b on 32b hosts and 64b on 64b hosts? If I'm recalling that correctly, then this patch results in a harmless (though unnecessary) sign extension for 32b targets. That should be fine, but someone like Arnd should triple check if my concern is valid or not.
Quoted 3 lines
It could actually be 'int', 'long' or 'long long' depending on the architecture and C library. Maybe we need a temporary variable of type 'long long' to hold the difference, and pass that to
llabs()?
Yeah, that SGTM. Thanks for the review!
Signature · 2 lines
Thanks, ~Nick Desaulniers
PATCH v2 1/1 selftests: timens: exec: use 'labs()' over 'abs()'
In reply to Arnd BergmannWhen 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)
^~~
labsRework 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.
--- tools/testing/selftests/timens/exec.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
tools/testing/selftests/timens/exec.c +7 -3
@@ -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); }
Signature · 1 line
2.33.0
Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 12 lines · Anders Roxell
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
Quoted 6 lines
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 <[email protected]>Signed-off-by Anders Roxell <[email protected]>
Thanks for the patch!
Quoted 49 lines
--- tools/testing/selftests/timens/exec.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)tools/testing/selftests/timens/exec.c +7
-4@@ -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
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Anders Roxell
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) ^~~ labsRework 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.
Quoted 5 lines
Quoted 2 lines
Suggested-by Arnd Bergmann <[email protected]>Signed-off-by Anders Roxell <[email protected]>Thanks for the patch!
Reviewed-by Nick Desaulniers <[email protected]>
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?
Quoted 55 lines
Quoted 48 lines
--- tools/testing/selftests/timens/exec.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)tools/testing/selftests/timens/exec.c +7
-4@@ -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); } --Quoted 2 lines
2.33.0
--
Thanks,
~Nick Desaulniers
Signature · 2 lines
Thanks, ~Nick Desaulniers
Re: [PATCHv2] selftests: timens: exec: use 'labs()' over 'abs()'
Quoted 21 lines · Nick Desaulniers
Quoted 18 lines · Nick Desaulniers
Quoted 16 lines · Anders Roxell
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) ^~~ labsRework 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.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
Suggested-by Arnd Bergmann <[email protected]>Signed-off-by Anders Roxell <[email protected]>Thanks for the patch!
Reviewed-by Nick Desaulniers <[email protected]>ah, gmail doesn't do a great job at showing the subject when a v2 is sent in-reply-to.
oh right, sorry.
Quoted 2 lines
Should the oneline mention llabs rather than labs
now?
You are correct, can this be changed when the patch gets applied or should I send a v3?
Cheers,
Anders
Quoted 12 lines · Anders Roxell
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
Quoted 3 lines
The note indicates what to do, Rework to use the function '
llabs()'.Signed-off-by Anders Roxell <[email protected]>
Thanks for the patch!
Quoted 34 lines
--- tools/testing/selftests/timens/timer.c | 2 +- tools/testing/selftests/timens/timerfd.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)tools/testing/selftests/timens/timer.c +1
-1@@ -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 ;tools/testing/selftests/timens/timerfd.c +1
-2@@ -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
Signature · 2 lines
Thanks, ~Nick Desaulniers
Shuah Khan
maintainerQuoted 45 lines · Nick Desaulniers
Quoted 12 lines · Anders Roxell
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) { ^~~ llabsQuoted 3 lines
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]>Quoted 25 lines
--- tools/testing/selftests/timens/timer.c | 2 +- tools/testing/selftests/timens/timerfd.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)tools/testing/selftests/timens/timer.c +1
-1@@ -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 ;tools/testing/selftests/timens/timerfd.c +0
-0@@ -61,7 +61,7 @@ int run_test(int clockid, struct timespec now)return pr_perror ("timerfd_gettime(%d)" , clockid);
Quoted 11 lines
Quoted 9 lines
tools/testing/selftests/timens/timerfd.c +1
-2elapsed = 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