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

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: d1934ed9803c ("binder: encapsulate individual alloc test cases")
Suggested-by: Kees Cook <kees@kernel.org>
Cc: Joel Fernandes <joelagnelf@nvidia.com>
Signed-off-by: Tiffany Yang <ynaffit@google.com>
Reviewed-by: Kees Cook <kees@kernel.org>
Link: https://lore.kernel.org/r/20250722234508.232228-2-ynaffit@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Tiffany Yang and committed by
Greg Kroah-Hartman
fa3f79e8 8a8d47e8

+21 -25
+21 -25
drivers/android/tests/binder_alloc_kunit.c
··· 15 15 #include <linux/fs.h> 16 16 #include <linux/mm.h> 17 17 #include <linux/mman.h> 18 + #include <linux/seq_buf.h> 18 19 #include <linux/sizes.h> 19 20 20 21 #include "../binder_alloc.h" ··· 108 107 }; 109 108 110 109 struct binder_alloc_test_case_info { 110 + char alignments[ALIGNMENTS_BUFLEN]; 111 + struct seq_buf alignments_sb; 111 112 size_t *buffer_sizes; 112 113 int *free_sequence; 113 - char alignments[ALIGNMENTS_BUFLEN]; 114 114 bool front_pages; 115 115 }; 116 116 117 - static void stringify_free_seq(struct kunit *test, int *seq, char *buf, 118 - size_t buf_len) 117 + static void stringify_free_seq(struct kunit *test, int *seq, struct seq_buf *sb) 119 118 { 120 - size_t bytes = 0; 121 119 int i; 122 120 123 - for (i = 0; i < BUFFER_NUM; i++) { 124 - bytes += snprintf(buf + bytes, buf_len - bytes, "[%d]", seq[i]); 125 - if (bytes >= buf_len) 126 - break; 127 - } 128 - KUNIT_EXPECT_LT(test, bytes, buf_len); 121 + for (i = 0; i < BUFFER_NUM; i++) 122 + seq_buf_printf(sb, "[%d]", seq[i]); 123 + 124 + KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb)); 129 125 } 130 126 131 127 static void stringify_alignments(struct kunit *test, int *alignments, 132 - char *buf, size_t buf_len) 128 + struct seq_buf *sb) 133 129 { 134 - size_t bytes = 0; 135 130 int i; 136 131 137 - for (i = 0; i < BUFFER_NUM; i++) { 138 - bytes += snprintf(buf + bytes, buf_len - bytes, "[ %d:%s ]", i, 139 - buf_end_align_type_strs[alignments[i]]); 140 - if (bytes >= buf_len) 141 - break; 142 - } 132 + for (i = 0; i < BUFFER_NUM; i++) 133 + seq_buf_printf(sb, "[ %d:%s ]", i, 134 + buf_end_align_type_strs[alignments[i]]); 143 135 144 - KUNIT_EXPECT_LT(test, bytes, buf_len); 136 + KUNIT_EXPECT_FALSE(test, seq_buf_has_overflowed(sb)); 145 137 } 146 138 147 139 static bool check_buffer_pages_allocated(struct kunit *test, ··· 305 311 int i; 306 312 307 313 if (index == BUFFER_NUM) { 308 - char freeseq_buf[FREESEQ_BUFLEN]; 314 + DECLARE_SEQ_BUF(freeseq_sb, FREESEQ_BUFLEN); 309 315 310 316 case_failed = binder_alloc_test_alloc_free(test, alloc, tc, end); 311 317 *runs += 1; 312 318 *failures += case_failed; 313 319 314 320 if (case_failed || PRINT_ALL_CASES) { 315 - stringify_free_seq(test, tc->free_sequence, freeseq_buf, 316 - FREESEQ_BUFLEN); 321 + stringify_free_seq(test, tc->free_sequence, 322 + &freeseq_sb); 317 323 kunit_err(test, "case %lu: [%s] | %s - %s - %s", *runs, 318 324 case_failed ? "FAILED" : "PASSED", 319 325 tc->front_pages ? "front" : "back ", 320 - tc->alignments, freeseq_buf); 326 + seq_buf_str(&tc->alignments_sb), 327 + seq_buf_str(&freeseq_sb)); 321 328 } 322 329 323 330 return; ··· 378 383 if (index == BUFFER_NUM) { 379 384 struct binder_alloc_test_case_info tc = {0}; 380 385 381 - stringify_alignments(test, alignments, tc.alignments, 382 - ALIGNMENTS_BUFLEN); 386 + seq_buf_init(&tc.alignments_sb, tc.alignments, 387 + ALIGNMENTS_BUFLEN); 388 + stringify_alignments(test, alignments, &tc.alignments_sb); 383 389 384 390 gen_buf_sizes(test, alloc, &tc, end_offset, runs, failures); 385 391 return;