From 655f91691934a744638bf1a8dbc60f4095a1b83b Mon Sep 17 00:00:00 2001 From: Oleg Nesterov Date: Fri, 28 Jun 2013 09:53:52 +1000 Subject: [PATCH] coredump: cn_vprintf() has no reason to call vsnprintf() twice cn_vprintf() looks really overcomplicated and sub-optimal. We do not need vsnprintf(NULL) to calculate the size we need, we can simply try to print into the current buffer and expand/retry only if necessary. Signed-off-by: Oleg Nesterov Cc: Andi Kleen Cc: Colin Walters Cc: Denys Vlasenko Cc: Jiri Slaby Cc: Lennart Poettering Cc: Lucas De Marchi Acked-by: Neil Horman Signed-off-by: Andrew Morton --- fs/coredump.c | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/fs/coredump.c b/fs/coredump.c index 5413e9b90bb4..68652a0ebaea 100644 --- a/fs/coredump.c +++ b/fs/coredump.c @@ -71,26 +71,20 @@ static int expand_corename(struct core_name *cn) static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg) { - char *cur; - int need; - int ret; - - need = vsnprintf(NULL, 0, fmt, arg); - if (likely(need < cn->size - cn->used - 1)) - goto out_printf; - - ret = expand_corename(cn); - if (ret) - goto expand_fail; + int free, need; + +again: + free = cn->size - cn->used; + need = vsnprintf(cn->corename + cn->used, free, fmt, arg); + if (need < free) { + cn->used += need; + return 0; + } -out_printf: - cur = cn->corename + cn->used; - vsnprintf(cur, need + 1, fmt, arg); - cn->used += need; - return 0; + if (!expand_corename(cn)) + goto again; -expand_fail: - return ret; + return -ENOMEM; } static int cn_printf(struct core_name *cn, const char *fmt, ...) -- 2.39.5