@@ -1291,11 +1291,16 @@ static size_t info_print_prefix(const struct printk_info *info, bool syslog,
12911291 * done:
12921292 *
12931293 * - Add prefix for each line.
1294+ * - Drop truncated lines that no longer fit into the buffer.
12941295 * - Add the trailing newline that has been removed in vprintk_store().
1295- * - Drop truncated lines that do not longer fit into the buffer.
1296+ * - Add a string terminator.
1297+ *
1298+ * Since the produced string is always terminated, the maximum possible
1299+ * return value is @r->text_buf_size - 1;
12961300 *
12971301 * Return: The length of the updated/prepared text, including the added
1298- * prefixes and the newline. The dropped line(s) are not counted.
1302+ * prefixes and the newline. The terminator is not counted. The dropped
1303+ * line(s) are not counted.
12991304 */
13001305static size_t record_print_text (struct printk_record * r , bool syslog ,
13011306 bool time )
@@ -1338,26 +1343,31 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13381343
13391344 /*
13401345 * Truncate the text if there is not enough space to add the
1341- * prefix and a trailing newline.
1346+ * prefix and a trailing newline and a terminator .
13421347 */
1343- if (len + prefix_len + text_len + 1 > buf_size ) {
1348+ if (len + prefix_len + text_len + 1 + 1 > buf_size ) {
13441349 /* Drop even the current line if no space. */
1345- if (len + prefix_len + line_len + 1 > buf_size )
1350+ if (len + prefix_len + line_len + 1 + 1 > buf_size )
13461351 break ;
13471352
1348- text_len = buf_size - len - prefix_len - 1 ;
1353+ text_len = buf_size - len - prefix_len - 1 - 1 ;
13491354 truncated = true;
13501355 }
13511356
13521357 memmove (text + prefix_len , text , text_len );
13531358 memcpy (text , prefix , prefix_len );
13541359
1360+ /*
1361+ * Increment the prepared length to include the text and
1362+ * prefix that were just moved+copied. Also increment for the
1363+ * newline at the end of this line. If this is the last line,
1364+ * there is no newline, but it will be added immediately below.
1365+ */
13551366 len += prefix_len + line_len + 1 ;
1356-
13571367 if (text_len == line_len ) {
13581368 /*
1359- * Add the trailing newline removed in
1360- * vprintk_store().
1369+ * This is the last line. Add the trailing newline
1370+ * removed in vprintk_store().
13611371 */
13621372 text [prefix_len + line_len ] = '\n' ;
13631373 break ;
@@ -1382,6 +1392,14 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
13821392 text_len -= line_len + 1 ;
13831393 }
13841394
1395+ /*
1396+ * If a buffer was provided, it will be terminated. Space for the
1397+ * string terminator is guaranteed to be available. The terminator is
1398+ * not counted in the return value.
1399+ */
1400+ if (buf_size > 0 )
1401+ text [len ] = 0 ;
1402+
13851403 return len ;
13861404}
13871405
@@ -3427,7 +3445,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
34273445 while (prb_read_valid_info (prb , seq , & info , & line_count )) {
34283446 if (r .info -> seq >= dumper -> next_seq )
34293447 break ;
3430- l += get_record_print_text_size (& info , line_count , true , time );
3448+ l += get_record_print_text_size (& info , line_count , syslog , time );
34313449 seq = r .info -> seq + 1 ;
34323450 }
34333451
@@ -3437,7 +3455,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
34373455 & info , & line_count )) {
34383456 if (r .info -> seq >= dumper -> next_seq )
34393457 break ;
3440- l -= get_record_print_text_size (& info , line_count , true , time );
3458+ l -= get_record_print_text_size (& info , line_count , syslog , time );
34413459 seq = r .info -> seq + 1 ;
34423460 }
34433461
0 commit comments