Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 79f2b5d

Browse files
committed
Fix valgrind's "unaddressable bytes" whining about BRIN code.
brin_form_tuple calculated an exact tuple size, then palloc'd and filled just that much. Later, brin_doinsert or brin_doupdate would MAXALIGN the tuple size and tell PageAddItem that that was the size of the tuple to insert. If the original tuple size wasn't a multiple of MAXALIGN, the net result would be that PageAddItem would memcpy a few more bytes than the palloc request had been for. AFAICS, this is totally harmless in the real world: the error is a read overrun not a write overrun, and palloc would certainly have rounded the request up to a MAXALIGN multiple internally, so there's no chance of the memcpy fetching off the end of memory. Valgrind, however, is picky to the byte level not the MAXALIGN level. Fix it by pushing the MAXALIGN step back to brin_form_tuple. (The other possible source of tuples in this code, brin_form_placeholder_tuple, was already producing a MAXALIGN'd result.) In passing, be a bit more paranoid about internal allocations in brin_form_tuple.
1 parent 3503003 commit 79f2b5d

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/backend/access/brin/brin_pageops.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ brin_doupdate(Relation idxrel, BlockNumber pagesPerRange,
5555
Buffer newbuf;
5656
bool extended = false;
5757

58-
newsz = MAXALIGN(newsz);
58+
Assert(newsz == MAXALIGN(newsz));
5959

6060
/* make sure the revmap is long enough to contain the entry we need */
6161
brinRevmapExtend(revmap, heapBlk);
@@ -273,7 +273,7 @@ brin_doinsert(Relation idxrel, BlockNumber pagesPerRange,
273273
ItemPointerData tid;
274274
bool extended = false;
275275

276-
itemsz = MAXALIGN(itemsz);
276+
Assert(itemsz == MAXALIGN(itemsz));
277277

278278
/* Make sure the revmap is long enough to contain the entry we need */
279279
brinRevmapExtend(revmap, heapBlk);

src/backend/access/brin/brin_tuple.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
103103

104104
Assert(brdesc->bd_totalstored > 0);
105105

106-
values = palloc(sizeof(Datum) * brdesc->bd_totalstored);
107-
nulls = palloc0(sizeof(bool) * brdesc->bd_totalstored);
108-
phony_nullbitmap = palloc(sizeof(bits8) * BITMAPLEN(brdesc->bd_totalstored));
106+
values = (Datum *) palloc(sizeof(Datum) * brdesc->bd_totalstored);
107+
nulls = (bool *) palloc0(sizeof(bool) * brdesc->bd_totalstored);
108+
phony_nullbitmap = (bits8 *)
109+
palloc(sizeof(bits8) * BITMAPLEN(brdesc->bd_totalstored));
109110

110111
/*
111112
* Set up the values/nulls arrays for heap_fill_tuple
@@ -144,6 +145,9 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
144145
values[idxattno++] = tuple->bt_columns[keyno].bv_values[datumno];
145146
}
146147

148+
/* Assert we did not overrun temp arrays */
149+
Assert(idxattno <= brdesc->bd_totalstored);
150+
147151
/* compute total space needed */
148152
len = SizeOfBrinTuple;
149153
if (anynulls)
@@ -160,12 +164,15 @@ brin_form_tuple(BrinDesc *brdesc, BlockNumber blkno, BrinMemTuple *tuple,
160164

161165
data_len = heap_compute_data_size(brtuple_disk_tupdesc(brdesc),
162166
values, nulls);
163-
164167
len += data_len;
165168

169+
len = MAXALIGN(len);
170+
166171
rettuple = palloc0(len);
167172
rettuple->bt_blkno = blkno;
168173
rettuple->bt_info = hoff;
174+
175+
/* Assert that hoff fits in the space available */
169176
Assert((rettuple->bt_info & BRIN_OFFSET_MASK) == hoff);
170177

171178
/*

0 commit comments

Comments
 (0)