Skip to content

Commit b7fc1dd

Browse files
committed
Fix failure to consider failure cases in GetComboCommandId().
Failure to initially palloc the comboCids array, or to realloc it bigger when needed, left combocid's data structures in an inconsistent state that would cause trouble if the top transaction continues to execute. Noted while examining a user complaint about the amount of memory used for this. (There's not much we can do about that, but it does point up that repalloc failure has a non-negligible chance of occurring here.) In HEAD/9.5, also avoid possible invocation of memcpy() with a null pointer in SerializeComboCIDState; cf commit 13bba02.
1 parent 3d357b4 commit b7fc1dd

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/backend/utils/time/combocid.c

+20-18
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
215215
{
216216
HASHCTL hash_ctl;
217217

218+
/* Make array first; existence of hash table asserts array exists */
219+
comboCids = (ComboCidKeyData *)
220+
MemoryContextAlloc(TopTransactionContext,
221+
sizeof(ComboCidKeyData) * CCID_ARRAY_SIZE);
222+
sizeComboCids = CCID_ARRAY_SIZE;
223+
usedComboCids = 0;
224+
218225
memset(&hash_ctl, 0, sizeof(hash_ctl));
219226
hash_ctl.keysize = sizeof(ComboCidKeyData);
220227
hash_ctl.entrysize = sizeof(ComboCidEntryData);
@@ -225,12 +232,20 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
225232
CCID_HASH_SIZE,
226233
&hash_ctl,
227234
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
235+
}
236+
237+
/*
238+
* Grow the array if there's not at least one free slot. We must do this
239+
* before possibly entering a new hashtable entry, else failure to
240+
* repalloc would leave a corrupt hashtable entry behind.
241+
*/
242+
if (usedComboCids >= sizeComboCids)
243+
{
244+
int newsize = sizeComboCids * 2;
228245

229246
comboCids = (ComboCidKeyData *)
230-
MemoryContextAlloc(TopTransactionContext,
231-
sizeof(ComboCidKeyData) * CCID_ARRAY_SIZE);
232-
sizeComboCids = CCID_ARRAY_SIZE;
233-
usedComboCids = 0;
247+
repalloc(comboCids, sizeof(ComboCidKeyData) * newsize);
248+
sizeComboCids = newsize;
234249
}
235250

236251
/* Lookup or create a hash entry with the desired cmin/cmax */
@@ -249,20 +264,7 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
249264
return entry->combocid;
250265
}
251266

252-
/*
253-
* We have to create a new combo cid. Check that there's room for it in
254-
* the array, and grow it if there isn't.
255-
*/
256-
if (usedComboCids >= sizeComboCids)
257-
{
258-
/* We need to grow the array */
259-
int newsize = sizeComboCids * 2;
260-
261-
comboCids = (ComboCidKeyData *)
262-
repalloc(comboCids, sizeof(ComboCidKeyData) * newsize);
263-
sizeComboCids = newsize;
264-
}
265-
267+
/* We have to create a new combo cid; we already made room in the array */
266268
combocid = usedComboCids;
267269

268270
comboCids[combocid].cmin = cmin;

0 commit comments

Comments
 (0)