Skip to content

Commit bde2f18

Browse files
committed
Fix edge-case for xl_tot_len broken by bae868c.
bae868c removed a check that was still needed. If you had an xl_tot_len at the end of a page that was too small for a record header, but not big enough to span onto the next page, we'd immediately perform the CRC check using a bogus large length. Because of arbitrary coding differences between the CRC implementations on different platforms, nothing very bad happened on common modern systems. On systems using the _sb8.c fallback we could segfault. Restore that check, add a new assertion and supply a test for that case. Back-patch to 12, like bae868c. Tested-by: Tom Lane <tgl@sss.pgh.pa.us> Tested-by: Alexander Lakhin <exclusion@gmail.com> Discussion: https://postgr.es/m/CA%2BhUKGLCkTT7zYjzOxuLGahBdQ%3DMcF%3Dz5ZvrjSOnW4EDhVjT-g%40mail.gmail.com
1 parent fd7a114 commit bde2f18

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/backend/access/transam/xlogreader.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,15 @@ XLogReadRecord(XLogReaderState *state, XLogRecPtr RecPtr, char **errormsg)
330330
}
331331
else
332332
{
333+
/* There may be no next page if it's too small. */
334+
if (total_len < SizeOfXLogRecord)
335+
{
336+
report_invalid_record(state,
337+
"invalid record length at %X/%X: wanted %u, got %u",
338+
(uint32) (RecPtr >> 32), (uint32) RecPtr,
339+
(uint32) SizeOfXLogRecord, total_len);
340+
goto err;
341+
}
333342
/* We'll validate the header once we have the next page. */
334343
gotheader = false;
335344
}
@@ -745,6 +754,8 @@ ValidXLogRecord(XLogReaderState *state, XLogRecord *record, XLogRecPtr recptr)
745754
{
746755
pg_crc32c crc;
747756

757+
Assert(record->xl_tot_len >= SizeOfXLogRecord);
758+
748759
/* Calculate the CRC */
749760
INIT_CRC32C(crc);
750761
COMP_CRC32C(crc, ((char *) record) + SizeOfXLogRecord, record->xl_tot_len - SizeOfXLogRecord);

src/test/recovery/t/039_end_of_wal.pl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,19 @@ sub advance_to_record_splitting_zone
287287
$log_size),
288288
"xl_tot_len short");
289289

290+
# xl_tot_len in final position, not big enough to span into a new page but
291+
# also not eligible for regular record header validation
292+
emit_message($node, 0);
293+
$end_lsn = advance_to_record_splitting_zone($node);
294+
$node->stop('immediate');
295+
write_wal($node, $TLI, $end_lsn, build_record_header(1));
296+
$log_size = -s $node->logfile;
297+
$node->start;
298+
ok( $node->log_contains(
299+
"invalid record length at .*: wanted 24, got 1", $log_size
300+
),
301+
"xl_tot_len short at end-of-page");
302+
290303
# Need more pages, but xl_prev check fails first.
291304
emit_message($node, 0);
292305
$end_lsn = advance_out_of_record_splitting_zone($node);

0 commit comments

Comments
 (0)