Skip to content

Commit e3e84fd

Browse files
committed
Further improvements to c8f621c.
Coverity and inspection for the issue addressed in fd45d16 found some questionable code. Specifically coverity noticed that the wrong length was added in ReorderBufferSerializeChange() - without immediate negative consequences as the variable isn't used afterwards. During code-review and testing I noticed that a bit of space was wasted when allocating tuple bufs in several places. Thirdly, the debug memset()s in ReorderBufferGetTupleBuf() reduce the error checking valgrind can do. Backpatch: 9.4, like c8f621c.
1 parent 89f8372 commit e3e84fd

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/backend/replication/logical/decode.c

+15-9
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,16 @@ DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
625625

626626
if (xlrec->flags & XLOG_HEAP_CONTAINS_NEW_TUPLE)
627627
{
628-
Size tuplelen = r->xl_len - SizeOfHeapInsert;
628+
Size datalen = r->xl_len - SizeOfHeapInsert;
629+
Size tuplelen = datalen - SizeOfHeapHeader;
629630

630631
Assert(r->xl_len > (SizeOfHeapInsert + SizeOfHeapHeader));
631632

632633
change->data.tp.newtuple =
633634
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
634635

635636
DecodeXLogTuple((char *) xlrec + SizeOfHeapInsert,
636-
tuplelen, change->data.tp.newtuple);
637+
datalen, change->data.tp.newtuple);
637638
}
638639

639640
change->data.tp.clear_toast_afterwards = true;
@@ -670,6 +671,7 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
670671

671672
if (xlrec->flags & XLOG_HEAP_CONTAINS_NEW_TUPLE)
672673
{
674+
Size datalen;
673675
Size tuplelen;
674676
xl_heap_header_len xlhdr;
675677

@@ -678,12 +680,13 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
678680
memcpy(&xlhdr, data, sizeof(xlhdr));
679681
data += offsetof(xl_heap_header_len, header);
680682

681-
tuplelen = xlhdr.t_len + SizeOfHeapHeader;
683+
datalen = xlhdr.t_len + SizeOfHeapHeader;
684+
tuplelen = xlhdr.t_len;
682685

683686
change->data.tp.newtuple =
684687
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
685688

686-
DecodeXLogTuple(data, tuplelen, change->data.tp.newtuple);
689+
DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
687690
/* skip over the rest of the tuple header */
688691
data += SizeOfHeapHeader;
689692
/* skip over the tuple data */
@@ -692,18 +695,20 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
692695

693696
if (xlrec->flags & XLOG_HEAP_CONTAINS_OLD)
694697
{
698+
Size datalen;
695699
Size tuplelen;
696700
xl_heap_header_len xlhdr;
697701

698702
memcpy(&xlhdr, data, sizeof(xlhdr));
699703
data += offsetof(xl_heap_header_len, header);
700704

701-
tuplelen = xlhdr.t_len + SizeOfHeapHeader;
705+
datalen = xlhdr.t_len + SizeOfHeapHeader;
706+
tuplelen = xlhdr.t_len;
702707

703708
change->data.tp.oldtuple =
704709
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
705710

706-
DecodeXLogTuple(data, tuplelen, change->data.tp.oldtuple);
711+
DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
707712
#ifdef NOT_USED
708713
data += SizeOfHeapHeader;
709714
data += xlhdr.t_len;
@@ -741,15 +746,16 @@ DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
741746
/* old primary key stored */
742747
if (xlrec->flags & XLOG_HEAP_CONTAINS_OLD)
743748
{
744-
Size len = r->xl_len - SizeOfHeapDelete;
749+
Size datalen = r->xl_len - SizeOfHeapDelete;
750+
Size tuplelen = datalen - SizeOfHeapHeader;
745751

746752
Assert(r->xl_len > (SizeOfHeapDelete + SizeOfHeapHeader));
747753

748754
change->data.tp.oldtuple =
749-
ReorderBufferGetTupleBuf(ctx->reorder, len);
755+
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
750756

751757
DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
752-
len, change->data.tp.oldtuple);
758+
datalen, change->data.tp.oldtuple);
753759
}
754760

755761
change->data.tp.clear_toast_afterwards = true;

src/backend/replication/logical/reorderbuffer.c

+3
Original file line numberDiff line numberDiff line change
@@ -469,12 +469,15 @@ ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
469469
rb->nr_cached_tuplebufs--;
470470
tuple = slist_container(ReorderBufferTupleBuf, node,
471471
slist_pop_head_node(&rb->cached_tuplebufs));
472+
Assert(tuple->alloc_tuple_size == MaxHeapTupleSize);
472473
#ifdef USE_ASSERT_CHECKING
473474
memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
475+
VALGRIND_MAKE_MEM_UNDEFINED(&tuple->tuple, sizeof(HeapTupleData));
474476
#endif
475477
tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
476478
#ifdef USE_ASSERT_CHECKING
477479
memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
480+
VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
478481
#endif
479482
}
480483
else

0 commit comments

Comments
 (0)