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

Commit 8a3f745

Browse files
committed
Do not access indclass through Form_pg_index
Normally, accessing variable-length members of catalog structures past the first one doesn't work at all. Here, it happened to work because indnatts was checked to be 1, and so the defined FormData_pg_index layout, using int2vector[1] and oidvector[1] for variable-length arrays, happened to match the actual memory layout. But it's a very fragile assumption, and it's not in a performance-critical path, so code it properly using heap_getattr() instead. bug analysis by Tom Lane
1 parent eb6af01 commit 8a3f745

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/backend/utils/cache/relcache.c

+16-1
Original file line numberDiff line numberDiff line change
@@ -3351,15 +3351,30 @@ RelationGetIndexList(Relation relation)
33513351
while (HeapTupleIsValid(htup = systable_getnext(indscan)))
33523352
{
33533353
Form_pg_index index = (Form_pg_index) GETSTRUCT(htup);
3354+
Datum indclassDatum;
3355+
oidvector *indclass;
3356+
bool isnull;
33543357

33553358
/* Add index's OID to result list in the proper order */
33563359
result = insert_ordered_oid(result, index->indexrelid);
33573360

3361+
/*
3362+
* indclass cannot be referenced directly through the C struct, because
3363+
* it comes after the variable-width indkey field. Must extract the
3364+
* datum the hard way...
3365+
*/
3366+
indclassDatum = heap_getattr(htup,
3367+
Anum_pg_index_indclass,
3368+
GetPgIndexDescriptor(),
3369+
&isnull);
3370+
Assert(!isnull);
3371+
indclass = (oidvector *) DatumGetPointer(indclassDatum);
3372+
33583373
/* Check to see if it is a unique, non-partial btree index on OID */
33593374
if (index->indnatts == 1 &&
33603375
index->indisunique && index->indimmediate &&
33613376
index->indkey.values[0] == ObjectIdAttributeNumber &&
3362-
index->indclass.values[0] == OID_BTREE_OPS_OID &&
3377+
indclass->values[0] == OID_BTREE_OPS_OID &&
33633378
heap_attisnull(htup, Anum_pg_index_indpred))
33643379
oidIndex = index->indexrelid;
33653380
}

0 commit comments

Comments
 (0)