test_printf: Append strings more efficiently
authorMatthew Wilcox (Oracle) <willy@infradead.org>
Tue, 19 Oct 2021 14:26:20 +0000 (15:26 +0100)
committerPetr Mladek <pmladek@suse.com>
Wed, 27 Oct 2021 11:40:13 +0000 (13:40 +0200)
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
lib/test_printf.c

index ec58419..d09993f 100644 (file)
@@ -614,8 +614,7 @@ 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;
        }
 
@@ -623,17 +622,14 @@ page_flags_test(int section, int node, int zone, int last_cpupid,
                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;
        }