Skip to content

Commit 5866730

Browse files
tamirdkees
authored andcommitted
scanf: implicate test line in failure messages
This improves the failure output by pointing to the failing line at the top level of the test. Reviewed-by: Petr Mladek <[email protected]> Tested-by: Petr Mladek <[email protected]> Signed-off-by: Tamir Duberstein <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 034bee6 commit 5866730

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

lib/test_scanf.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ static char *test_buffer __initdata;
2424
static char *fmt_buffer __initdata;
2525
static struct rnd_state rnd_state __initdata;
2626

27-
typedef int (*check_fn)(const void *check_data, const char *string,
28-
const char *fmt, int n_args, va_list ap);
27+
typedef int (*check_fn)(const char *file, const int line, const void *check_data,
28+
const char *string, const char *fmt, int n_args, va_list ap);
2929

30-
static void __scanf(4, 6) __init
31-
_test(check_fn fn, const void *check_data, const char *string, const char *fmt,
32-
int n_args, ...)
30+
static void __scanf(6, 8) __init
31+
_test(const char *file, const int line, check_fn fn, const void *check_data, const char *string,
32+
const char *fmt, int n_args, ...)
3333
{
3434
va_list ap, ap_copy;
3535
int ret;
@@ -42,12 +42,12 @@ _test(check_fn fn, const void *check_data, const char *string, const char *fmt,
4242
va_end(ap_copy);
4343

4444
if (ret != n_args) {
45-
pr_warn("vsscanf(\"%s\", \"%s\", ...) returned %d expected %d\n",
46-
string, fmt, ret, n_args);
45+
pr_warn("%s:%d: vsscanf(\"%s\", \"%s\", ...) returned %d expected %d\n",
46+
file, line, string, fmt, ret, n_args);
4747
goto fail;
4848
}
4949

50-
ret = (*fn)(check_data, string, fmt, n_args, ap);
50+
ret = (*fn)(file, line, check_data, string, fmt, n_args, ap);
5151
if (ret)
5252
goto fail;
5353

@@ -67,88 +67,88 @@ do { \
6767
typeof(*expect) got = *va_arg(ap, typeof(expect)); \
6868
pr_debug("\t" arg_fmt "\n", got); \
6969
if (got != *expect) { \
70-
pr_warn("vsscanf(\"%s\", \"%s\", ...) expected " arg_fmt " got " arg_fmt "\n", \
71-
str, fmt, *expect, got); \
70+
pr_warn("%s:%d, vsscanf(\"%s\", \"%s\", ...) expected " arg_fmt " got " arg_fmt "\n", \
71+
file, line, str, fmt, *expect, got); \
7272
return 1; \
7373
} \
7474
} \
7575
return 0; \
7676
} while (0)
7777

78-
static int __init check_ull(const void *check_data, const char *string,
79-
const char *fmt, int n_args, va_list ap)
78+
static int __init check_ull(const char *file, const int line, const void *check_data,
79+
const char *string, const char *fmt, int n_args, va_list ap)
8080
{
8181
const unsigned long long *pval = check_data;
8282

8383
_check_numbers_template("%llu", pval, string, fmt, n_args, ap);
8484
}
8585

86-
static int __init check_ll(const void *check_data, const char *string,
87-
const char *fmt, int n_args, va_list ap)
86+
static int __init check_ll(const char *file, const int line, const void *check_data,
87+
const char *string, const char *fmt, int n_args, va_list ap)
8888
{
8989
const long long *pval = check_data;
9090

9191
_check_numbers_template("%lld", pval, string, fmt, n_args, ap);
9292
}
9393

94-
static int __init check_ulong(const void *check_data, const char *string,
95-
const char *fmt, int n_args, va_list ap)
94+
static int __init check_ulong(const char *file, const int line, const void *check_data,
95+
const char *string, const char *fmt, int n_args, va_list ap)
9696
{
9797
const unsigned long *pval = check_data;
9898

9999
_check_numbers_template("%lu", pval, string, fmt, n_args, ap);
100100
}
101101

102-
static int __init check_long(const void *check_data, const char *string,
103-
const char *fmt, int n_args, va_list ap)
102+
static int __init check_long(const char *file, const int line, const void *check_data,
103+
const char *string, const char *fmt, int n_args, va_list ap)
104104
{
105105
const long *pval = check_data;
106106

107107
_check_numbers_template("%ld", pval, string, fmt, n_args, ap);
108108
}
109109

110-
static int __init check_uint(const void *check_data, const char *string,
111-
const char *fmt, int n_args, va_list ap)
110+
static int __init check_uint(const char *file, const int line, const void *check_data,
111+
const char *string, const char *fmt, int n_args, va_list ap)
112112
{
113113
const unsigned int *pval = check_data;
114114

115115
_check_numbers_template("%u", pval, string, fmt, n_args, ap);
116116
}
117117

118-
static int __init check_int(const void *check_data, const char *string,
119-
const char *fmt, int n_args, va_list ap)
118+
static int __init check_int(const char *file, const int line, const void *check_data,
119+
const char *string, const char *fmt, int n_args, va_list ap)
120120
{
121121
const int *pval = check_data;
122122

123123
_check_numbers_template("%d", pval, string, fmt, n_args, ap);
124124
}
125125

126-
static int __init check_ushort(const void *check_data, const char *string,
127-
const char *fmt, int n_args, va_list ap)
126+
static int __init check_ushort(const char *file, const int line, const void *check_data,
127+
const char *string, const char *fmt, int n_args, va_list ap)
128128
{
129129
const unsigned short *pval = check_data;
130130

131131
_check_numbers_template("%hu", pval, string, fmt, n_args, ap);
132132
}
133133

134-
static int __init check_short(const void *check_data, const char *string,
135-
const char *fmt, int n_args, va_list ap)
134+
static int __init check_short(const char *file, const int line, const void *check_data,
135+
const char *string, const char *fmt, int n_args, va_list ap)
136136
{
137137
const short *pval = check_data;
138138

139139
_check_numbers_template("%hd", pval, string, fmt, n_args, ap);
140140
}
141141

142-
static int __init check_uchar(const void *check_data, const char *string,
143-
const char *fmt, int n_args, va_list ap)
142+
static int __init check_uchar(const char *file, const int line, const void *check_data,
143+
const char *string, const char *fmt, int n_args, va_list ap)
144144
{
145145
const unsigned char *pval = check_data;
146146

147147
_check_numbers_template("%hhu", pval, string, fmt, n_args, ap);
148148
}
149149

150-
static int __init check_char(const void *check_data, const char *string,
151-
const char *fmt, int n_args, va_list ap)
150+
static int __init check_char(const char *file, const int line, const void *check_data,
151+
const char *string, const char *fmt, int n_args, va_list ap)
152152
{
153153
const signed char *pval = check_data;
154154

@@ -196,7 +196,7 @@ do { \
196196
T result = ~expect_val; /* should be overwritten */ \
197197
\
198198
snprintf(test_buffer, BUF_SIZE, gen_fmt, expect_val); \
199-
_test(fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \
199+
_test(__FILE__, __LINE__, fn, &expect_val, test_buffer, "%" scan_fmt, 1, &result); \
200200
} while (0)
201201

202202
#define simple_numbers_loop(T, gen_fmt, scan_fmt, fn) \
@@ -344,7 +344,7 @@ static void __init append_delim(char *str_buf, int *str_buf_pos, int str_buf_len
344344
#define test_array_8(fn, check_data, string, fmt, arr) \
345345
do { \
346346
BUILD_BUG_ON(ARRAY_SIZE(arr) != 8); \
347-
_test(fn, check_data, string, fmt, 8, \
347+
_test(__FILE__, __LINE__, fn, check_data, string, fmt, 8, \
348348
&(arr)[0], &(arr)[1], &(arr)[2], &(arr)[3], \
349349
&(arr)[4], &(arr)[5], &(arr)[6], &(arr)[7]); \
350350
} while (0)
@@ -608,7 +608,7 @@ do { \
608608
const T expect[2] = { expect0, expect1 }; \
609609
T result[2] = { (T)~expect[0], (T)~expect[1] }; \
610610
\
611-
_test(fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]); \
611+
_test(__FILE__, __LINE__, fn, &expect, str, scan_fmt, n_args, &result[0], &result[1]); \
612612
} while (0)
613613

614614
/*

0 commit comments

Comments
 (0)