Skip to content

Commit 5306df2

Browse files
committed
Clean up foreign-key caching code in planner.
Coverity complained that the code added by 015e889 lacked an error check for SearchSysCache1 failures, which it should have. But the code was pretty duff in other ways too, including failure to think about whether it could really cope with arrays of different lengths.
1 parent 7e3bb08 commit 5306df2

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/backend/optimizer/util/plancat.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,20 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
408408

409409
foreach(l, fkoidlist)
410410
{
411-
int i;
412-
ArrayType *arr;
411+
Oid fkoid = lfirst_oid(l);
412+
HeapTuple htup;
413+
Form_pg_constraint constraint;
414+
ForeignKeyOptInfo *info;
413415
Datum adatum;
414416
bool isnull;
417+
ArrayType *arr;
415418
int numkeys;
416-
Oid fkoid = lfirst_oid(l);
417-
418-
HeapTuple htup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(fkoid));
419-
Form_pg_constraint constraint = (Form_pg_constraint) GETSTRUCT(htup);
419+
int i;
420420

421-
ForeignKeyOptInfo *info;
421+
htup = SearchSysCache1(CONSTROID, ObjectIdGetDatum(fkoid));
422+
if (!HeapTupleIsValid(htup)) /* should not happen */
423+
elog(ERROR, "cache lookup failed for constraint %u", fkoid);
424+
constraint = (Form_pg_constraint) GETSTRUCT(htup);
422425

423426
Assert(constraint->contype == CONSTRAINT_FOREIGN);
424427

@@ -434,8 +437,7 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
434437

435438
arr = DatumGetArrayTypeP(adatum);
436439
numkeys = ARR_DIMS(arr)[0];
437-
info->conkeys = (int*)palloc0(numkeys * sizeof(int));
438-
440+
info->conkeys = (int*)palloc(numkeys * sizeof(int));
439441
for (i = 0; i < numkeys; i++)
440442
info->conkeys[i] = ((int16 *) ARR_DATA_PTR(arr))[i];
441443

@@ -445,9 +447,8 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
445447
Assert(!isnull);
446448

447449
arr = DatumGetArrayTypeP(adatum);
448-
numkeys = ARR_DIMS(arr)[0];
449-
info->confkeys = (int*)palloc0(numkeys * sizeof(int));
450-
450+
Assert(numkeys == ARR_DIMS(arr)[0]);
451+
info->confkeys = (int*)palloc(numkeys * sizeof(int));
451452
for (i = 0; i < numkeys; i++)
452453
info->confkeys[i] = ((int16 *) ARR_DATA_PTR(arr))[i];
453454

@@ -457,17 +458,16 @@ get_relation_info(PlannerInfo *root, Oid relationObjectId, bool inhparent,
457458
Assert(!isnull);
458459

459460
arr = DatumGetArrayTypeP(adatum);
460-
numkeys = ARR_DIMS(arr)[0];
461-
info->conpfeqop = (Oid*)palloc0(numkeys * sizeof(Oid));
462-
461+
Assert(numkeys == ARR_DIMS(arr)[0]);
462+
info->conpfeqop = (Oid*)palloc(numkeys * sizeof(Oid));
463463
for (i = 0; i < numkeys; i++)
464464
info->conpfeqop[i] = ((Oid *) ARR_DATA_PTR(arr))[i];
465465

466466
info->nkeys = numkeys;
467467

468468
ReleaseSysCache(htup);
469469

470-
fkinfos = lcons(info, fkinfos);
470+
fkinfos = lappend(fkinfos, info);
471471
}
472472

473473
list_free(fkoidlist);

0 commit comments

Comments
 (0)