Skip to content

Commit 5fe9d8c

Browse files
oleg-nesterovtorvalds
authored andcommitted
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 <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> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bc03c69 commit 5fe9d8c

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

‎fs/coredump.c‎

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -71,26 +71,20 @@ static int expand_corename(struct core_name *cn)
7171

7272
static int cn_vprintf(struct core_name *cn, const char *fmt, va_list arg)
7373
{
74-
char *cur;
75-
int need;
76-
int ret;
77-
78-
need = vsnprintf(NULL, 0, fmt, arg);
79-
if (likely(need < cn->size - cn->used - 1))
80-
goto out_printf;
81-
82-
ret = expand_corename(cn);
83-
if (ret)
84-
goto expand_fail;
74+
int free, need;
75+
76+
again:
77+
free = cn->size - cn->used;
78+
need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
79+
if (need < free) {
80+
cn->used += need;
81+
return 0;
82+
}
8583

86-
out_printf:
87-
cur = cn->corename + cn->used;
88-
vsnprintf(cur, need + 1, fmt, arg);
89-
cn->used += need;
90-
return 0;
84+
if (!expand_corename(cn))
85+
goto again;
9186

92-
expand_fail:
93-
return ret;
87+
return -ENOMEM;
9488
}
9589

9690
static int cn_printf(struct core_name *cn, const char *fmt, ...)

0 commit comments

Comments
 (0)