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

From: John Ogness
Date: Tue Jan 03 2023 - 05:22:08 EST


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. ;-)

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.
*/

John