From: Oleg Nesterov on
On 08/02, Xiaotian Feng wrote:
>
> @@ -1466,78 +1496,126 @@ static int format_corename(char *corename, long signr)
> goto out;
> /* Double percent, output one percent */
> case '%':
> - if (out_ptr == out_end)
> - goto out;
> *out_ptr++ = '%';

Hmm. Not sure I understand why we do not need to check the space here.

> - rc = snprintf(out_ptr, out_end - out_ptr,
> - "%d", task_tgid_vnr(current));
> - if (rc > out_end - out_ptr)
> - goto out;
> + rc = snprintf(NULL, 0, "%d",
> + task_tgid_vnr(current));
> + if (rc > out_end - out_ptr) {
> + ret = expand_corename(corename,
> + &out_end,
> + &out_ptr, &size);
> + if (ret)
> + return ret;
> + }
> + rc = snprintf(out_ptr, rc + 1, "%d",
> + task_tgid_vnr(current));

Probably it makes sense to factor out this code?

Roughly, something like:

struct core_name {
char *corename;
int len, free;
};

static bool cn_printf(struct core_name *cn, const char *fmt, ...)
{
char *cur;
int need;
va_list ap;

retry:
cur = cn->corename + (cn->len - cn->free);
need = vsnprintf(cur, cn->free, fmt, ap);

if (likely(need < free)) {
free -= need;
return true;
}

increase ->len, realloc ->corename or return false;
goto retry;

}


Then format_corename() can just do

if (!cn_printf(&cn, ...))
return -ENOMEM;

consistently.



Also. Not sure this really makes sense, but if we ever need to expand the
string, perhaps it makes sense to remeber this fact so that the next time
we start with len > CORENAME_MAX_SIZE. In any case, I think this needs a
separate patch.

Oleg.

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo(a)vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/