Re: [PATCH printk v3 6/6] printk: introduce console_prepend_dropped() for dropped messages

From: Petr Mladek
Date: Tue Jan 03 2023 - 08:29:38 EST


On Tue 2023-01-03 11:26:23, John Ogness wrote:
> On 2023-01-02, Petr Mladek <pmladek@xxxxxxxx> wrote:
> >> +static void console_prepend_dropped(struct console_message *cmsg, unsigned long dropped)
> >> +{
> >> + struct console_buffers *cbufs = cmsg->cbufs;
> >> + const size_t scratchbuf_sz = sizeof(cbufs->scratchbuf);
> >> + const size_t outbuf_sz = sizeof(cbufs->outbuf);
> >> + char *scratchbuf = &cbufs->scratchbuf[0];
> >> + char *outbuf = &cbufs->outbuf[0];
> >> + size_t len;
> >> +
> >> + len = snprintf(scratchbuf, scratchbuf_sz,
> >> + "** %lu printk messages dropped **\n", dropped);
> >> +
> >> + /*
> >> + * Make sure outbuf is sufficiently large before prepending. Space
> >> + * for a terminator is also counted in case truncation occurs.
> >> + */
> >> + if (WARN_ON_ONCE(len + 1 >= outbuf_sz))
> >> + return;
> >
> > Strictly speaking, this should be:
> >
> > if (WARN_ON_ONCE(len >= outbuf_sz))
> > return;
> >
> > The trailing '\0' will fit into the buffer when len < outbuf_sz.
>
> You are correct.
>
> My concern was that if "@len = @outbuf_sz - 1", the entire message will
> be truncated and you will only see the dropped messages text. I wanted
> at least 1 character from the message to survive. ;-)

I see.

> How about the commments:
>
> /*
> * Make sure outbuf is sufficiently large before prepending. Space
> * for a terminator and at least 1 byte of the message is also
> * checked in case truncation is needed.
> */

It is better than the original.

Well, what about making sure that something more useful is always
printed. For example:

/*
* Make sure outbuf is sufficiently large before prepending.
* Keep at least the prefix when the message has to be truncated.
* It is a rather theoretical problem when someone tries to
* use a minimalist buffer.
*/
if (WARN_ON_ONCE(len + PREFIX_MAX + 1 >= outbuf_sz))
return;

If we want to use this way. It would probably make sense to
rename PREFIX_MAX to CONSOLE_PREFIX_MAX.

Best Regards,
Petr