mirror of
https://github.com/postgres/postgres.git
synced 2025-06-30 21:42:05 +03:00
Fix potential NULL pointer dereference in getIdentitySequence()
The function invokes SearchSysCacheAttNum() and SearchSysCacheAttName().
They may respectively return 0 for the attribute number or NULL for
the attribute name if the attribute does not exist, without any kind of
error handling. The common practice is to check that the data retrieved
from the syscache is valid. There is no risk of NULL pointer
dereferences currently, but let's stick to the practice of making sure
that this data is always valid, to catch future inconsistency mistakes.
The code is switched to use get_attnum() and get_attname(), and adds
some error handling.
Oversight in 509199587d
.
Reported-by: Ranier Vilela
Author: Ashutosh Bapat
Discussion: https://postgr.es/m/CAEudQAqh_RZqoFcYKso5d9VhF-Vd64_ZodfQ_2zSusszkEmyRg@mail.gmail.com
This commit is contained in:
@ -945,7 +945,7 @@ getOwnedSequences(Oid relid)
|
|||||||
Oid
|
Oid
|
||||||
getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
|
getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
|
||||||
{
|
{
|
||||||
Oid relid;
|
Oid relid = RelationGetRelid(rel);
|
||||||
List *seqlist;
|
List *seqlist;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -954,22 +954,16 @@ getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
|
|||||||
*/
|
*/
|
||||||
if (RelationGetForm(rel)->relispartition)
|
if (RelationGetForm(rel)->relispartition)
|
||||||
{
|
{
|
||||||
List *ancestors =
|
List *ancestors = get_partition_ancestors(relid);
|
||||||
get_partition_ancestors(RelationGetRelid(rel));
|
const char *attname = get_attname(relid, attnum, false);
|
||||||
HeapTuple ctup = SearchSysCacheAttNum(RelationGetRelid(rel), attnum);
|
|
||||||
const char *attname = NameStr(((Form_pg_attribute) GETSTRUCT(ctup))->attname);
|
|
||||||
HeapTuple ptup;
|
|
||||||
|
|
||||||
relid = llast_oid(ancestors);
|
relid = llast_oid(ancestors);
|
||||||
ptup = SearchSysCacheAttName(relid, attname);
|
attnum = get_attnum(relid, attname);
|
||||||
attnum = ((Form_pg_attribute) GETSTRUCT(ptup))->attnum;
|
if (attnum == InvalidAttrNumber)
|
||||||
|
elog(ERROR, "cache lookup failed for attribute \"%s\" of relation %u",
|
||||||
ReleaseSysCache(ctup);
|
attname, relid);
|
||||||
ReleaseSysCache(ptup);
|
|
||||||
list_free(ancestors);
|
list_free(ancestors);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
relid = RelationGetRelid(rel);
|
|
||||||
|
|
||||||
seqlist = getOwnedSequences_internal(relid, attnum, DEPENDENCY_INTERNAL);
|
seqlist = getOwnedSequences_internal(relid, attnum, DEPENDENCY_INTERNAL);
|
||||||
if (list_length(seqlist) > 1)
|
if (list_length(seqlist) > 1)
|
||||||
|
Reference in New Issue
Block a user