Skip to contentSkip to conversation

selftests: timers: use 'llabs()' over 'abs()'

PATCH In review Anders Roxell 3 participants 3 messages Nov 5, 2021 – Nov 20, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: timers: use 'llabs()' over 'abs()'

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

posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]

        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^
posix_timers.c:69:6: note: use function 'llabs' instead
        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^~~
            llabs

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

---
 tools/testing/selftests/timers/posix_timers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/timers/posix_timers.c +1 -1
index 337424c5c987..73fb27901a1d 100644
@@ -66,7 +66,7 @@ static int check_diff(struct timeval start, struct timeval end)
 	diff = end.tv_usec - start.tv_usec;
 	diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
 
-	if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
+	if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
 		printf("Diff too high: %lld..", diff);
 		return -1;
 	}
Signature · 1 line
2.33.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 12 lines · Anders Roxell

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

posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]

        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^
posix_timers.c:69:6: note: use function 'llabs' instead
        if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
            ^~~
            llabs
Quoted 3 lines

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

Thanks for the patch!

I wonder why tools/testing/selftests/timers/adjtick.c redefines llabs when it already includes stdlib.h, and how that doesn't result in some kind of compiler diagnostic.

Quoted 20 lines
---
 tools/testing/selftests/timers/posix_timers.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/timers/posix_timers.c +1 -2
index 337424c5c987..73fb27901a1d 100644
@@ -66,7 +66,7 @@ static int check_diff(struct timeval start, struct timeval end)
        diff = end.tv_usec - start.tv_usec;
        diff += (end.tv_sec - start.tv_sec) * USECS_PER_SEC;
 
-       if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
+       if (llabs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
                printf("Diff too high: %lld..", diff);
                return -1;
        }
--

2.33.0

Signature · 2 lines
Thanks,
~Nick Desaulniers
maintainer
#2 lore raw
Quoted 15 lines · Nick Desaulniers
Quoted 14 lines · Anders Roxell

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

posix_timers.c:69:6: warning: absolute value function 'abs' given an argument of type 'long long' but has parameter of type 'int' which may cause truncation of value [-Wabsolute-value]

         if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
             ^
posix_timers.c:69:6: note: use function 'llabs' instead
         if (abs(diff - DELAY * USECS_PER_SEC) > USECS_PER_SEC / 2) {
             ^~~
             llabs

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

Quoted 9 lines
Quoted 1 line

Thanks for the patch!

I wonder why tools/testing/selftests/timers/adjtick.c redefines llabs when it already includes stdlib.h, and how that doesn't result in some kind of compiler diagnostic.

Hmm. The define in /usr/include/stdlib.h is under #ifdef __USE_ISOC99 Maybe be that explains the reason for the definitions in the other two timers file?

Anders, I would like to understand this before I take this patch. I see you sent several patches to other tests as well. Would be useful at least understand why these defines exist.

thanks,
-- Shuah