Re: [PATCH 1/2] remove all uses of printf's %n

From: Tetsuo Handa
Date: Mon Sep 16 2013 - 07:42:36 EST


Kees Cook wrote:
> - seq_printf(m, "%s%d%n", con->name, con->index, &len);
> - len = 21 - len;
> + len = m->count;
> + seq_printf(m, "%s%d", con->name, con->index);
> + len = 21 - (m->count - len);

Why not to create a new function which returns bytes written?
The new function does not need to return negative value for indicating errors.
---------- patch start ----------
diff --git a/include/linux/seq_file.h b/include/linux/seq_file.h
index 4e32edc..c889cf1 100644
--- a/include/linux/seq_file.h
+++ b/include/linux/seq_file.h
@@ -91,6 +91,7 @@ int seq_write(struct seq_file *seq, const void *data, size_t len);

__printf(2, 3) int seq_printf(struct seq_file *, const char *, ...);
__printf(2, 0) int seq_vprintf(struct seq_file *, const char *, va_list args);
+__printf(2, 3) int seq_new_printf(struct seq_file *m, const char *f, ...);

int seq_path(struct seq_file *, const struct path *, const char *);
int seq_dentry(struct seq_file *, struct dentry *, const char *);
diff --git a/fs/seq_file.c b/fs/seq_file.c
index 3135c25..7af75ec 100644
--- a/fs/seq_file.c
+++ b/fs/seq_file.c
@@ -419,6 +419,27 @@ int seq_printf(struct seq_file *m, const char *f, ...)
EXPORT_SYMBOL(seq_printf);

/**
+ * seq_new_printf - seq_printf() which returns bytes written.
+ * @m: target buffer
+ * @f: format
+ *
+ * Returns bytes written to @m.
+ */
+int seq_new_printf(struct seq_file *m, const char *f, ...)
+{
+ const int count = m->count;
+ int ret;
+ va_list args;
+
+ va_start(args, f);
+ ret = seq_vprintf(m, f, args);
+ va_end(args);
+
+ return ret ? 0 : m->count - count;
+}
+EXPORT_SYMBOL(seq_new_printf);
+
+/**
* mangle_path - mangle and copy path to buffer beginning
* @s: buffer start
* @p: beginning of path in above buffer
---------- patch end ----------
With new function, we can do:

- len = m->count;
- seq_printf(m, "%s%d", con->name, con->index);
- len = 21 - (m->count - len);
+ len = 21 - seq_new_printf(m, "%s%d", con->name, con->index);
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/