@@ -1291,11 +1291,16 @@ static size_t info_print_prefix(const struct printk_info *info, bool syslog,
1291
1291
* done:
1292
1292
*
1293
1293
* - Add prefix for each line.
1294
+ * - Drop truncated lines that no longer fit into the buffer.
1294
1295
* - 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;
1296
1300
*
1297
1301
* 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.
1299
1304
*/
1300
1305
static size_t record_print_text (struct printk_record * r , bool syslog ,
1301
1306
bool time )
@@ -1338,26 +1343,31 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
1338
1343
1339
1344
/*
1340
1345
* 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 .
1342
1347
*/
1343
- if (len + prefix_len + text_len + 1 > buf_size ) {
1348
+ if (len + prefix_len + text_len + 1 + 1 > buf_size ) {
1344
1349
/* 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 )
1346
1351
break ;
1347
1352
1348
- text_len = buf_size - len - prefix_len - 1 ;
1353
+ text_len = buf_size - len - prefix_len - 1 - 1 ;
1349
1354
truncated = true;
1350
1355
}
1351
1356
1352
1357
memmove (text + prefix_len , text , text_len );
1353
1358
memcpy (text , prefix , prefix_len );
1354
1359
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
+ */
1355
1366
len += prefix_len + line_len + 1 ;
1356
-
1357
1367
if (text_len == line_len ) {
1358
1368
/*
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().
1361
1371
*/
1362
1372
text [prefix_len + line_len ] = '\n' ;
1363
1373
break ;
@@ -1382,6 +1392,14 @@ static size_t record_print_text(struct printk_record *r, bool syslog,
1382
1392
text_len -= line_len + 1 ;
1383
1393
}
1384
1394
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
+
1385
1403
return len ;
1386
1404
}
1387
1405
@@ -3427,7 +3445,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3427
3445
while (prb_read_valid_info (prb , seq , & info , & line_count )) {
3428
3446
if (r .info -> seq >= dumper -> next_seq )
3429
3447
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 );
3431
3449
seq = r .info -> seq + 1 ;
3432
3450
}
3433
3451
@@ -3437,7 +3455,7 @@ bool kmsg_dump_get_buffer(struct kmsg_dumper *dumper, bool syslog,
3437
3455
& info , & line_count )) {
3438
3456
if (r .info -> seq >= dumper -> next_seq )
3439
3457
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 );
3441
3459
seq = r .info -> seq + 1 ;
3442
3460
}
3443
3461
0 commit comments