8000 Fix failure to consider failure cases in GetComboCommandId(). · home201448/postgres@6823bc2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6823bc2

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 64b7079 commit 6823bc2

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

src/backend/utils/time/combocid.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,13 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
207207
{
208208
HASHCTL hash_ctl;
209209

210+
/* Make array first; existence of hash table asserts array exists */
211+
comboCids = (ComboCidKeyData *)
212+
MemoryContextAlloc(TopTransactionContext,
213+
sizeof(ComboCidKeyData) * CCID_ARRAY_SIZE);
214+
sizeComboCids = CCID_ARRAY_SIZE;
215+
usedComboCids = 0;
216+
210217
memset(&hash_ctl, 0, sizeof(hash_ctl));
211218
hash_ctl.keysize = sizeof(ComboCidKeyData);
212219
hash_ctl.entrysize = sizeof(ComboCidEntryData);
@@ -217,12 +224,20 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
217224
CCID_HASH_SIZE,
218225
&hash_ctl,
219226
HASH_ELEM | HASH_FUNCTION | HASH_CONTEXT);
227+
}
228+
229+
/*
230+
* Grow the array if there's not at least one free slot. We must do this
231+
* before possibly entering a new hashtable entry, else failure to
232+
* repalloc would leave a corrupt hashtable entry behind.
233+
*/
234+
if (usedComboCids >= sizeComboCids)
235+
{
236+
int newsize = sizeComboCids * 2;
220237

221238
comboCids = (ComboCidKeyData *)
222-
MemoryContextAlloc(TopTransactionContext,
223-
sizeof(ComboCidKeyData) * CCID_ARRAY_SIZE);
224-
sizeComboCids = CCID_ARRAY_SIZE;
225-
usedComboCids = 0;
239+
repalloc(comboCids, sizeof(ComboCidKeyData) * newsize);
240+
sizeComboCids = newsize;
226241
}
227242

228243
/* Lookup or create a hash entry with the desired cmin/cmax */
@@ -241,20 +256,7 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
241256
return entry->combocid;
242257
}
243258

244-
/*
245-
* We have to create a new combo cid. Check that there's room for it in
246-
* the array, and grow it if there isn't.
247-
*/
248-
if (usedComboCids >= sizeComboCids)
249-
{
250-
/* We need to grow the array */
251-
int newsize = sizeComboCids * 2;
252-
253-
comboCids = (ComboCidKeyData *)
254-
repalloc(comboCids, sizeof(ComboCidKeyData) * newsize);
255-
sizeComboCids = newsize;
256-
}
257-
259+
/* We have to create a new combo cid; we already made room in the array */
258260
combocid = usedComboCids;
259261

260262
comboCids[combocid].cmin = cmin;

0 commit comments

Comments
 (0)
0