Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kselftest: save-and-restore errno to allow for %m formatting

Previously, using "%m" in a ksft_* format string can result in strange
output because the errno value wasn't saved before calling other libc
functions. The solution is to simply save and restore the errno before
we format the user-supplied format string.

Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Aleksa Sarai and committed by
Shuah Khan
fc2e634e 527d37e9

+15
+15
tools/testing/selftests/kselftest.h
··· 10 10 #ifndef __KSELFTEST_H 11 11 #define __KSELFTEST_H 12 12 13 + #include <errno.h> 13 14 #include <stdlib.h> 14 15 #include <unistd.h> 15 16 #include <stdarg.h> ··· 82 81 83 82 static inline void ksft_print_msg(const char *msg, ...) 84 83 { 84 + int saved_errno = errno; 85 85 va_list args; 86 86 87 87 va_start(args, msg); 88 88 printf("# "); 89 + errno = saved_errno; 89 90 vprintf(msg, args); 90 91 va_end(args); 91 92 } 92 93 93 94 static inline void ksft_test_result_pass(const char *msg, ...) 94 95 { 96 + int saved_errno = errno; 95 97 va_list args; 96 98 97 99 ksft_cnt.ksft_pass++; 98 100 99 101 va_start(args, msg); 100 102 printf("ok %d ", ksft_test_num()); 103 + errno = saved_errno; 101 104 vprintf(msg, args); 102 105 va_end(args); 103 106 } 104 107 105 108 static inline void ksft_test_result_fail(const char *msg, ...) 106 109 { 110 + int saved_errno = errno; 107 111 va_list args; 108 112 109 113 ksft_cnt.ksft_fail++; 110 114 111 115 va_start(args, msg); 112 116 printf("not ok %d ", ksft_test_num()); 117 + errno = saved_errno; 113 118 vprintf(msg, args); 114 119 va_end(args); 115 120 } 116 121 117 122 static inline void ksft_test_result_skip(const char *msg, ...) 118 123 { 124 + int saved_errno = errno; 119 125 va_list args; 120 126 121 127 ksft_cnt.ksft_xskip++; 122 128 123 129 va_start(args, msg); 124 130 printf("not ok %d # SKIP ", ksft_test_num()); 131 + errno = saved_errno; 125 132 vprintf(msg, args); 126 133 va_end(args); 127 134 } 128 135 129 136 static inline void ksft_test_result_error(const char *msg, ...) 130 137 { 138 + int saved_errno = errno; 131 139 va_list args; 132 140 133 141 ksft_cnt.ksft_error++; 134 142 135 143 va_start(args, msg); 136 144 printf("not ok %d # error ", ksft_test_num()); 145 + errno = saved_errno; 137 146 vprintf(msg, args); 138 147 va_end(args); 139 148 } ··· 163 152 164 153 static inline int ksft_exit_fail_msg(const char *msg, ...) 165 154 { 155 + int saved_errno = errno; 166 156 va_list args; 167 157 168 158 va_start(args, msg); 169 159 printf("Bail out! "); 160 + errno = saved_errno; 170 161 vprintf(msg, args); 171 162 va_end(args); 172 163 ··· 191 178 static inline int ksft_exit_skip(const char *msg, ...) 192 179 { 193 180 if (msg) { 181 + int saved_errno = errno; 194 182 va_list args; 195 183 196 184 va_start(args, msg); 197 185 printf("not ok %d # SKIP ", 1 + ksft_test_num()); 186 + errno = saved_errno; 198 187 vprintf(msg, args); 199 188 va_end(args); 200 189 } else {