lkml.org 
[lkml]   [2023]   [Jan]   [3]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: [PATCH printk v3 6/6] printk: introduce console_prepend_dropped() for dropped messages
Date
On 2023-01-02, Petr Mladek <pmladek@suse.com> 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

\
 
 \ /
  Last update: 2023-03-26 23:25    [W:0.131 / U:0.084 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site