]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
coredump: cn_vprintf() has no reason to call vsnprintf() twice
authorOleg Nesterov <oleg@redhat.com>
Thu, 27 Jun 2013 23:53:52 +0000 (09:53 +1000)
committerStephen Rothwell <sfr@canb.auug.org.au>
Fri, 28 Jun 2013 06:38:56 +0000 (16:38 +1000)
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 <oleg@redhat.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: Colin Walters <walters@verbum.org>
Cc: Denys Vlasenko <vda.linux@googlemail.com>
Cc: Jiri Slaby <jslaby@suse.cz>
Cc: Lennart Poettering <mzxreary@0pointer.de>
Cc: Lucas De Marchi <lucas.de.marchi@gmail.com>
Acked-by: Neil Horman <nhorman@tuxdriver.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
fs/coredump.c

index 5413e9b90bb475686031aea9093519d0e0735599..68652a0ebaeac2aa7bbd8af535ce33769e1f5f19 100644 (file)
@@ -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, ...)