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 <[email protected]>
Cc: Andi Kleen <[email protected]>
Cc: Colin Walters <[email protected]>
Cc: Denys Vlasenko <[email protected]>
Cc: Jiri Slaby <[email protected]>
Cc: Lennart Poettering <[email protected]>
Cc: Lucas De Marchi <[email protected]>
Acked-by: Neil Horman <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
This commit is contained in:
Oleg Nesterov
2013-07-03 16:08:02 -07:00
committed by Linus Torvalds
parent bc03c691aa
commit 5fe9d8ca21
+11 -17
View 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;
int free, need;
need = vsnprintf(NULL, 0, fmt, arg);
if (likely(need < cn->size - cn->used - 1))
goto out_printf;
again:
free = cn->size - cn->used;
need = vsnprintf(cn->corename + cn->used, free, fmt, arg);
if (need < free) {
cn->used += need;
return 0;
}
ret = expand_corename(cn);
if (ret)
goto expand_fail;
if (!expand_corename(cn))
goto again;
out_printf:
cur = cn->corename + cn->used;
vsnprintf(cur, need + 1, fmt, arg);
cn->used += need;
return 0;
expand_fail:
return ret;
return -ENOMEM;
}
static int cn_printf(struct core_name *cn, const char *fmt, ...)