Skip to content

Commit fa3f79e

Browse files
Tiffany Yanggregkh
authored andcommitted
binder: Use seq_buf in binder_alloc kunit tests
Replace instances of snprintf with seq_buf functions, as suggested by Kees [1]. [1] https://lore.kernel.org/all/202507160743.15E8044@keescook/ Fixes: d1934ed ("binder: encapsulate individual alloc test cases") Suggested-by: Kees Cook <[email protected]> Cc: Joel Fernandes <[email protected]> Signed-off-by: Tiffany Yang <[email protected]> Reviewed-by: Kees Cook <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 8a8d47e commit fa3f79e

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

drivers/android/tests/binder_alloc_kunit.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <linux/fs.h>
1616
#include <linux/mm.h>
1717
#include <linux/mman.h>
18+
#include <linux/seq_buf.h>
1819
#include <linux/sizes.h>
1920

2021
#include "../binder_alloc.h"
@@ -107,40 +108,33 @@ static const char *const buf_end_align_type_strs[LOOP_END] = {
107108
};
108109

109110
struct binder_alloc_test_case_info {
111+
char alignments[ALIGNMENTS_BUFLEN];
112+
struct seq_buf alignments_sb;
110113
size_t *buffer_sizes;
111114
int *free_sequence;
112-
char alignments[ALIGNMENTS_BUFLEN];
113115
bool front_pages;
114116
};
115117

116-
static void stringify_free_seq(struct kunit *test, int *seq, char *buf,
117-
size_t buf_len)
118+
static void stringify_free_seq(struct kunit *test, int *seq, struct seq_buf *sb)
118119
{
119-
size_t bytes = 0;
120120
int i;
121121

122-
for (i = 0; i < BUFFER_NUM; i++) {
123-
bytes += snprintf(buf + bytes, buf_len - bytes, "[%d]", seq[i]);
124-
if (bytes >= buf_len)
125-
break;
126-
}
127-
KUNIT_EXPECT_LT(test, bytes, buf_len);
122+
for (i = 0; i < BUFFER_NUM; i++)
123+
seq_buf_printf(sb, "[%d]", seq[i]);
124+
125+
KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
128126
}
129127

130128
static void stringify_alignments(struct kunit *test, int *alignments,
131-
char *buf, size_t buf_len)
129+
struct seq_buf *sb)
132130
{
133-
size_t bytes = 0;
134131
int i;
135132

136-
for (i = 0; i < BUFFER_NUM; i++) {
137-
bytes += snprintf(buf + bytes, buf_len - bytes, "[ %d:%s ]", i,
138-
buf_end_align_type_strs[alignments[i]]);
139-
if (bytes >= buf_len)
140-
break;
141-
}
133+
for (i = 0; i < BUFFER_NUM; i++)
134+
seq_buf_printf(sb, "[ %d:%s ]", i,
135+
buf_end_align_type_strs[alignments[i]]);
142136

143-
KUNIT_EXPECT_LT(test, bytes, buf_len);
137+
KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb));
144138
}
145139

146140
static bool check_buffer_pages_allocated(struct kunit *test,
@@ -311,19 +305,20 @@ static void permute_frees(struct kunit *test, struct binder_alloc *alloc,
311305
int i;
312306

313307
if (index == BUFFER_NUM) {
314-
char freeseq_buf[FREESEQ_BUFLEN];
308+
DECLARE_SEQ_BUF(freeseq_sb, FREESEQ_BUFLEN);
315309

316310
case_failed = binder_alloc_test_alloc_free(test, alloc, tc, end);
317311
*runs += 1;
318312
*failures += case_failed;
319313

320314
if (case_failed || PRINT_ALL_CASES) {
321-
stringify_free_seq(test, tc->free_sequence, freeseq_buf,
322-
FREESEQ_BUFLEN);
315+
stringify_free_seq(test, tc->free_sequence,
316+
&freeseq_sb);
323317
kunit_err(test, "case %lu: [%s] | %s - %s - %s", *runs,
324318
case_failed ? "FAILED" : "PASSED",
325319
tc->front_pages ? "front" : "back ",
326-
tc->alignments, freeseq_buf);
320+
seq_buf_str(&tc->alignments_sb),
321+
seq_buf_str(&freeseq_sb));
327322
}
328323

329324
return;
@@ -383,8 +378,9 @@ static void gen_buf_offsets(struct kunit *test, struct binder_alloc *alloc,
383378
if (index == BUFFER_NUM) {
384379
struct binder_alloc_test_case_info tc = {0};
385380

386-
stringify_alignments(test, alignments, tc.alignments,
387-
ALIGNMENTS_BUFLEN);
381+
seq_buf_init(&tc.alignments_sb, tc.alignments,
382+
ALIGNMENTS_BUFLEN);
383+
stringify_alignments(test, alignments, &tc.alignments_sb);
388384

389385
gen_buf_sizes(test, alloc, &tc, end_offset, runs, failures);
390386
return;

0 commit comments

Comments
 (0)