Skip to content

Commit d088c92

Browse files
keesshuahkh
authored andcommitted
selftests/harness: Display signed values correctly
Since forever the harness output for signed value tests have reported unsigned values to avoid casting. Instead, actually test the variable types and perform the correct casts and choose the correct format specifiers. Signed-off-by: Kees Cook <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 9847d24 commit d088c92

File tree

1 file changed

+37
-5
lines changed

1 file changed

+37
-5
lines changed

tools/testing/selftests/kselftest_harness.h

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -679,17 +679,49 @@
679679
if (_metadata->passed && _metadata->step < 255) \
680680
_metadata->step++;
681681

682+
#define is_signed_type(var) (!!(((__typeof__(var))(-1)) < (__typeof__(var))1))
683+
682684
#define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
683685
/* Avoid multiple evaluation of the cases */ \
684686
__typeof__(_expected) __exp = (_expected); \
685687
__typeof__(_seen) __seen = (_seen); \
686688
if (_assert) __INC_STEP(_metadata); \
687689
if (!(__exp _t __seen)) { \
688-
unsigned long long __exp_print = (uintptr_t)__exp; \
689-
unsigned long long __seen_print = (uintptr_t)__seen; \
690-
__TH_LOG("Expected %s (%llu) %s %s (%llu)", \
691-
_expected_str, __exp_print, #_t, \
692-
_seen_str, __seen_print); \
690+
/* Report with actual signedness to avoid weird output. */ \
691+
switch (is_signed_type(__exp) * 2 + is_signed_type(__seen)) { \
692+
case 0: { \
693+
unsigned long long __exp_print = (uintptr_t)__exp; \
694+
unsigned long long __seen_print = (uintptr_t)__seen; \
695+
__TH_LOG("Expected %s (%llu) %s %s (%llu)", \
696+
_expected_str, __exp_print, #_t, \
697+
_seen_str, __seen_print); \
698+
break; \
699+
} \
700+
case 1: { \
701+
unsigned long long __exp_print = (uintptr_t)__exp; \
702+
long long __seen_print = (intptr_t)__seen; \
703+
__TH_LOG("Expected %s (%llu) %s %s (%lld)", \
704+
_expected_str, __exp_print, #_t, \
705+
_seen_str, __seen_print); \
706+
break; \
707+
} \
708+
case 2: { \
709+
long long __exp_print = (intptr_t)__exp; \
710+
unsigned long long __seen_print = (uintptr_t)__seen; \
711+
__TH_LOG("Expected %s (%lld) %s %s (%llu)", \
712+
_expected_str, __exp_print, #_t, \
713+
_seen_str, __seen_print); \
714+
break; \
715+
} \
716+
case 3: { \
717+
long long __exp_print = (intptr_t)__exp; \
718+
long long __seen_print = (intptr_t)__seen; \
719+
__TH_LOG("Expected %s (%lld) %s %s (%lld)", \
720+
_expected_str, __exp_print, #_t, \
721+
_seen_str, __seen_print); \
722+
break; \
723+
} \
724+
} \
693725
_metadata->passed = 0; \
694726
/* Ensure the optional handler is triggered */ \
695727
_metadata->trigger = 1; \

0 commit comments

Comments
 (0)