Re: [RFC PATCH V2] core_pattern: fix long parameters was truncated by core_pattern handler

Oleg Nesterov
2010-08-02T13:53:30Z

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.