Skip to content

Commit

Permalink
test_printf: Append strings more efficiently
Browse files Browse the repository at this point in the history
Use scnprintf instead of snprintf + strlen.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Reviewed-by: Yafang Shao <laoar.shao@gmail.com>
Signed-off-by: Petr Mladek <pmladek@suse.com>
Link: https://lore.kernel.org/r/20211019142621.2810043-5-willy@infradead.org
  • Loading branch information
Matthew Wilcox (Oracle) authored and Petr Mladek committed Oct 27, 2021
1 parent 5b358b0 commit 507f986
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions lib/test_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -614,26 +614,22 @@ page_flags_test(int section, int node, int zone, int last_cpupid,
int i;

if (flags & PAGEFLAGS_MASK) {
snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
size = strlen(cmp_buf);
size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
append = true;
}

for (i = 0; i < ARRAY_SIZE(pft); i++) {
if (!pft[i].width)
continue;

if (append) {
snprintf(cmp_buf + size, BUF_SIZE - size, "|");
size = strlen(cmp_buf);
}
if (append)
size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");

flags |= (values[i] & pft[i].mask) << pft[i].shift;
snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name);
size = strlen(cmp_buf);
snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
values[i] & pft[i].mask);
size = strlen(cmp_buf);
size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
pft[i].name);
size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
values[i] & pft[i].mask);
append = true;
}

Expand Down

0 comments on commit 507f986

Please sign in to comment.