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

Skip to content

Commit cb7ea8d

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 6430a11 commit cb7ea8d

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
@@ -208,6 +208,13 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
208208
{
209209
HASHCTL hash_ctl;
210210

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

222239
comboCids = (ComboCidKeyData *)
223-
MemoryContextAlloc(TopTransactionContext,
224-
sizeof(ComboCidKeyData) * CCID_ARRAY_SIZE);
225-
sizeComboCids = CCID_ARRAY_SIZE;
226-
usedComboCids = 0;
240+
repalloc(comboCids, sizeof(ComboCidKeyData) * newsize);
241+
sizeComboCids = newsize;
227242
}
228243

229244
/* Lookup or create a hash entry with the desired cmin/cmax */
@@ -242,20 +257,7 @@ GetComboCommandId(CommandId cmin, CommandId cmax)
242257
return entry->combocid;
243258
}
244259

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

261263
comboCids[combocid].cmin = cmin;

0 commit comments

Comments
 (0)
0