1
0
mirror of https://github.com/postgres/postgres.git synced 2025-04-22 23:02:54 +03:00

Further improvements to c8f621c43.

Coverity and inspection for the issue addressed in fd45d16f 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 c8f621c43.
This commit is contained in:
Andres Freund 2016-03-07 14:24:03 -08:00
parent 3fc6e2d7f5
commit b63bea5fd3
2 changed files with 23 additions and 10 deletions

View File

@ -607,13 +607,14 @@ DecodeInsert(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE) if (xlrec->flags & XLH_INSERT_CONTAINS_NEW_TUPLE)
{ {
Size tuplelen; Size datalen;
char *tupledata = XLogRecGetBlockData(r, 0, &tuplelen); char *tupledata = XLogRecGetBlockData(r, 0, &datalen);
Size tuplelen = datalen - SizeOfHeapHeader;
change->data.tp.newtuple = change->data.tp.newtuple =
ReorderBufferGetTupleBuf(ctx->reorder, tuplelen); ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
DecodeXLogTuple(tupledata, tuplelen, change->data.tp.newtuple); DecodeXLogTuple(tupledata, datalen, change->data.tp.newtuple);
} }
change->data.tp.clear_toast_afterwards = true; change->data.tp.clear_toast_afterwards = true;
@ -634,7 +635,6 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
xl_heap_update *xlrec; xl_heap_update *xlrec;
ReorderBufferChange *change; ReorderBufferChange *change;
char *data; char *data;
Size datalen;
RelFileNode target_node; RelFileNode target_node;
xlrec = (xl_heap_update *) XLogRecGetData(r); xlrec = (xl_heap_update *) XLogRecGetData(r);
@ -655,22 +655,31 @@ DecodeUpdate(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
if (xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE) if (xlrec->flags & XLH_UPDATE_CONTAINS_NEW_TUPLE)
{ {
Size datalen;
Size tuplelen;
data = XLogRecGetBlockData(r, 0, &datalen); data = XLogRecGetBlockData(r, 0, &datalen);
tuplelen = datalen - SizeOfHeapHeader;
change->data.tp.newtuple = change->data.tp.newtuple =
ReorderBufferGetTupleBuf(ctx->reorder, datalen); ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
DecodeXLogTuple(data, datalen, change->data.tp.newtuple); DecodeXLogTuple(data, datalen, change->data.tp.newtuple);
} }
if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD) if (xlrec->flags & XLH_UPDATE_CONTAINS_OLD)
{ {
Size datalen;
Size tuplelen;
/* caution, remaining data in record is not aligned */ /* caution, remaining data in record is not aligned */
data = XLogRecGetData(r) + SizeOfHeapUpdate; data = XLogRecGetData(r) + SizeOfHeapUpdate;
datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate; datalen = XLogRecGetDataLen(r) - SizeOfHeapUpdate;
tuplelen = datalen - SizeOfHeapHeader;
change->data.tp.oldtuple = change->data.tp.oldtuple =
ReorderBufferGetTupleBuf(ctx->reorder, datalen); ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
DecodeXLogTuple(data, datalen, change->data.tp.oldtuple); DecodeXLogTuple(data, datalen, change->data.tp.oldtuple);
} }
@ -720,15 +729,16 @@ DecodeDelete(LogicalDecodingContext *ctx, XLogRecordBuffer *buf)
/* old primary key stored */ /* old primary key stored */
if (xlrec->flags & XLH_DELETE_CONTAINS_OLD) if (xlrec->flags & XLH_DELETE_CONTAINS_OLD)
{ {
Size len = XLogRecGetDataLen(r) - SizeOfHeapDelete; Size datalen = XLogRecGetDataLen(r) - SizeOfHeapDelete;
Size tuplelen = datalen - SizeOfHeapHeader;
Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader)); Assert(XLogRecGetDataLen(r) > (SizeOfHeapDelete + SizeOfHeapHeader));
change->data.tp.oldtuple = change->data.tp.oldtuple =
ReorderBufferGetTupleBuf(ctx->reorder, len); ReorderBufferGetTupleBuf(ctx->reorder, tuplelen);
DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete, DecodeXLogTuple((char *) xlrec + SizeOfHeapDelete,
len, change->data.tp.oldtuple); datalen, change->data.tp.oldtuple);
} }
change->data.tp.clear_toast_afterwards = true; change->data.tp.clear_toast_afterwards = true;

View File

@ -471,12 +471,15 @@ ReorderBufferGetTupleBuf(ReorderBuffer *rb, Size tuple_len)
rb->nr_cached_tuplebufs--; rb->nr_cached_tuplebufs--;
tuple = slist_container(ReorderBufferTupleBuf, node, tuple = slist_container(ReorderBufferTupleBuf, node,
slist_pop_head_node(&rb->cached_tuplebufs)); slist_pop_head_node(&rb->cached_tuplebufs));
Assert(tuple->alloc_tuple_size == MaxHeapTupleSize);
#ifdef USE_ASSERT_CHECKING #ifdef USE_ASSERT_CHECKING
memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData)); memset(&tuple->tuple, 0xa9, sizeof(HeapTupleData));
VALGRIND_MAKE_MEM_UNDEFINED(&tuple->tuple, sizeof(HeapTupleData));
#endif #endif
tuple->tuple.t_data = ReorderBufferTupleBufData(tuple); tuple->tuple.t_data = ReorderBufferTupleBufData(tuple);
#ifdef USE_ASSERT_CHECKING #ifdef USE_ASSERT_CHECKING
memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size); memset(tuple->tuple.t_data, 0xa8, tuple->alloc_tuple_size);
VALGRIND_MAKE_MEM_UNDEFINED(tuple->tuple.t_data, tuple->alloc_tuple_size);
#endif #endif
} }
else else
@ -2152,7 +2155,7 @@ ReorderBufferSerializeChange(ReorderBuffer *rb, ReorderBufferTXN *txn,
data += sizeof(HeapTupleData); data += sizeof(HeapTupleData);
memcpy(data, newtup->tuple.t_data, newlen); memcpy(data, newtup->tuple.t_data, newlen);
data += oldlen; data += newlen;
} }
break; break;
} }