diff options
author | Aleksa Sarai <cyphar@cyphar.com> | 2019-07-19 20:03:32 -0400 |
---|---|---|
committer | Shuah Khan <skhan@linuxfoundation.org> | 2019-07-30 17:46:48 -0400 |
commit | fc2e634e997d84f2610a482b500865ef2c04fcde (patch) | |
tree | 55e54ebeb07c4454cd47cbd9e76964fb011b3fb1 /tools | |
parent | 527d37e9e575bc0e9024de9b499385e7bb31f1ad (diff) |
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>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/testing/selftests/kselftest.h | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/tools/testing/selftests/kselftest.h b/tools/testing/selftests/kselftest.h index ec15c4f6af55..0ac49d91a260 100644 --- a/tools/testing/selftests/kselftest.h +++ b/tools/testing/selftests/kselftest.h | |||
@@ -10,6 +10,7 @@ | |||
10 | #ifndef __KSELFTEST_H | 10 | #ifndef __KSELFTEST_H |
11 | #define __KSELFTEST_H | 11 | #define __KSELFTEST_H |
12 | 12 | ||
13 | #include <errno.h> | ||
13 | #include <stdlib.h> | 14 | #include <stdlib.h> |
14 | #include <unistd.h> | 15 | #include <unistd.h> |
15 | #include <stdarg.h> | 16 | #include <stdarg.h> |
@@ -81,58 +82,68 @@ static inline void ksft_print_cnts(void) | |||
81 | 82 | ||
82 | static inline void ksft_print_msg(const char *msg, ...) | 83 | static inline void ksft_print_msg(const char *msg, ...) |
83 | { | 84 | { |
85 | int saved_errno = errno; | ||
84 | va_list args; | 86 | va_list args; |
85 | 87 | ||
86 | va_start(args, msg); | 88 | va_start(args, msg); |
87 | printf("# "); | 89 | printf("# "); |
90 | errno = saved_errno; | ||
88 | vprintf(msg, args); | 91 | vprintf(msg, args); |
89 | va_end(args); | 92 | va_end(args); |
90 | } | 93 | } |
91 | 94 | ||
92 | static inline void ksft_test_result_pass(const char *msg, ...) | 95 | static inline void ksft_test_result_pass(const char *msg, ...) |
93 | { | 96 | { |
97 | int saved_errno = errno; | ||
94 | va_list args; | 98 | va_list args; |
95 | 99 | ||
96 | ksft_cnt.ksft_pass++; | 100 | ksft_cnt.ksft_pass++; |
97 | 101 | ||
98 | va_start(args, msg); | 102 | va_start(args, msg); |
99 | printf("ok %d ", ksft_test_num()); | 103 | printf("ok %d ", ksft_test_num()); |
104 | errno = saved_errno; | ||
100 | vprintf(msg, args); | 105 | vprintf(msg, args); |
101 | va_end(args); | 106 | va_end(args); |
102 | } | 107 | } |
103 | 108 | ||
104 | static inline void ksft_test_result_fail(const char *msg, ...) | 109 | static inline void ksft_test_result_fail(const char *msg, ...) |
105 | { | 110 | { |
111 | int saved_errno = errno; | ||
106 | va_list args; | 112 | va_list args; |
107 | 113 | ||
108 | ksft_cnt.ksft_fail++; | 114 | ksft_cnt.ksft_fail++; |
109 | 115 | ||
110 | va_start(args, msg); | 116 | va_start(args, msg); |
111 | printf("not ok %d ", ksft_test_num()); | 117 | printf("not ok %d ", ksft_test_num()); |
118 | errno = saved_errno; | ||
112 | vprintf(msg, args); | 119 | vprintf(msg, args); |
113 | va_end(args); | 120 | va_end(args); |
114 | } | 121 | } |
115 | 122 | ||
116 | static inline void ksft_test_result_skip(const char *msg, ...) | 123 | static inline void ksft_test_result_skip(const char *msg, ...) |
117 | { | 124 | { |
125 | int saved_errno = errno; | ||
118 | va_list args; | 126 | va_list args; |
119 | 127 | ||
120 | ksft_cnt.ksft_xskip++; | 128 | ksft_cnt.ksft_xskip++; |
121 | 129 | ||
122 | va_start(args, msg); | 130 | va_start(args, msg); |
123 | printf("not ok %d # SKIP ", ksft_test_num()); | 131 | printf("not ok %d # SKIP ", ksft_test_num()); |
132 | errno = saved_errno; | ||
124 | vprintf(msg, args); | 133 | vprintf(msg, args); |
125 | va_end(args); | 134 | va_end(args); |
126 | } | 135 | } |
127 | 136 | ||
128 | static inline void ksft_test_result_error(const char *msg, ...) | 137 | static inline void ksft_test_result_error(const char *msg, ...) |
129 | { | 138 | { |
139 | int saved_errno = errno; | ||
130 | va_list args; | 140 | va_list args; |
131 | 141 | ||
132 | ksft_cnt.ksft_error++; | 142 | ksft_cnt.ksft_error++; |
133 | 143 | ||
134 | va_start(args, msg); | 144 | va_start(args, msg); |
135 | printf("not ok %d # error ", ksft_test_num()); | 145 | printf("not ok %d # error ", ksft_test_num()); |
146 | errno = saved_errno; | ||
136 | vprintf(msg, args); | 147 | vprintf(msg, args); |
137 | va_end(args); | 148 | va_end(args); |
138 | } | 149 | } |
@@ -152,10 +163,12 @@ static inline int ksft_exit_fail(void) | |||
152 | 163 | ||
153 | static inline int ksft_exit_fail_msg(const char *msg, ...) | 164 | static inline int ksft_exit_fail_msg(const char *msg, ...) |
154 | { | 165 | { |
166 | int saved_errno = errno; | ||
155 | va_list args; | 167 | va_list args; |
156 | 168 | ||
157 | va_start(args, msg); | 169 | va_start(args, msg); |
158 | printf("Bail out! "); | 170 | printf("Bail out! "); |
171 | errno = saved_errno; | ||
159 | vprintf(msg, args); | 172 | vprintf(msg, args); |
160 | va_end(args); | 173 | va_end(args); |
161 | 174 | ||
@@ -178,10 +191,12 @@ static inline int ksft_exit_xpass(void) | |||
178 | static inline int ksft_exit_skip(const char *msg, ...) | 191 | static inline int ksft_exit_skip(const char *msg, ...) |
179 | { | 192 | { |
180 | if (msg) { | 193 | if (msg) { |
194 | int saved_errno = errno; | ||
181 | va_list args; | 195 | va_list args; |
182 | 196 | ||
183 | va_start(args, msg); | 197 | va_start(args, msg); |
184 | printf("not ok %d # SKIP ", 1 + ksft_test_num()); | 198 | printf("not ok %d # SKIP ", 1 + ksft_test_num()); |
199 | errno = saved_errno; | ||
185 | vprintf(msg, args); | 200 | vprintf(msg, args); |
186 | va_end(args); | 201 | va_end(args); |
187 | } else { | 202 | } else { |