Skip to content

Commit fc2e634

Browse files
cypharshuahkh
authored andcommitted
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 <[email protected]> Signed-off-by: Shuah Khan <[email protected]>
1 parent 527d37e commit fc2e634

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

tools/testing/selftests/kselftest.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#ifndef __KSELFTEST_H
1111
#define __KSELFTEST_H
1212

13+
#include <errno.h>
1314
#include <stdlib.h>
1415
#include <unistd.h>
1516
#include <stdarg.h>
@@ -81,58 +82,68 @@ static inline void ksft_print_cnts(void)
8182

8283
static inline void ksft_print_msg(const char *msg, ...)
8384
{
85+
int saved_errno = errno;
8486
va_list args;
8587

8688
va_start(args, msg);
8789
printf("# ");
90+
errno = saved_errno;
8891
vprintf(msg, args);
8992
va_end(args);
9093
}
9194

9295
static inline void ksft_test_result_pass(const char *msg, ...)
9396
{
97+
int saved_errno = errno;
9498
va_list args;
9599

96100
ksft_cnt.ksft_pass++;
97101

98102
va_start(args, msg);
99103
printf("ok %d ", ksft_test_num());
104+
errno = saved_errno;
100105
vprintf(msg, args);
101106
va_end(args);
102107
}
103108

104109
static inline void ksft_test_result_fail(const char *msg, ...)
105110
{
111+
int saved_errno = errno;
106112
va_list args;
107113

108114
ksft_cnt.ksft_fail++;
109115

110116
va_start(args, msg);
111117
printf("not ok %d ", ksft_test_num());
118+
errno = saved_errno;
112119
vprintf(msg, args);
113120
va_end(args);
114121
}
115122

116123
static inline void ksft_test_result_skip(const char *msg, ...)
117124
{
125+
int saved_errno = errno;
118126
va_list args;
119127

120128
ksft_cnt.ksft_xskip++;
121129

122130
va_start(args, msg);
123131
printf("not ok %d # SKIP ", ksft_test_num());
132+
errno = saved_errno;
124133
vprintf(msg, args);
125134
va_end(args);
126135
}
127136

128137
static inline void ksft_test_result_error(const char *msg, ...)
129138
{
139+
int saved_errno = errno;
130140
va_list args;
131141

132142
ksft_cnt.ksft_error++;
133143

134144
va_start(args, msg);
135145
printf("not ok %d # error ", ksft_test_num());
146+
errno = saved_errno;
136147
vprintf(msg, args);
137148
va_end(args);
138149
}
@@ -152,10 +163,12 @@ static inline int ksft_exit_fail(void)
152163

153164
static inline int ksft_exit_fail_msg(const char *msg, ...)
154165
{
166+
int saved_errno = errno;
155167
va_list args;
156168

157169
va_start(args, msg);
158170
printf("Bail out! ");
171+
errno = saved_errno;
159172
vprintf(msg, args);
160173
va_end(args);
161174

@@ -178,10 +191,12 @@ static inline int ksft_exit_xpass(void)
178191
static inline int ksft_exit_skip(const char *msg, ...)
179192
{
180193
if (msg) {
194+
int saved_errno = errno;
181195
va_list args;
182196

183197
va_start(args, msg);
184198
printf("not ok %d # SKIP ", 1 + ksft_test_num());
199+
errno = saved_errno;
185200
vprintf(msg, args);
186201
va_end(args);
187202
} else {

0 commit comments

Comments
 (0)