summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2011-09-06 14:35:36 -0400
committerTom Lane <tgl@sss.pgh.pa.us>2011-09-06 14:37:48 -0400
commitad1e8274ebd8cff2a9fb44c6a941110a35989375 (patch)
tree465288acbd9e07bb8e02ec19cb3f63dba88cc48b
parentdcc728eef49bc0dbe2f88f9a56bda007afc9f46e (diff)
Avoid possibly accessing off the end of memory in examine_attribute().
Since the last couple of columns of pg_type are often NULL, sizeof(FormData_pg_type) can be an overestimate of the actual size of the tuple data part. Therefore memcpy'ing that much out of the catalog cache, as analyze.c was doing, poses a small risk of copying past the end of memory and incurring SIGSEGV. No such crash has been identified in the field, but we've certainly seen the equivalent happen in other code paths, so patch this one all the way back. Per valgrind testing by Noah Misch, though this is not his proposed patch. I chose to use SearchSysCacheCopy1 rather than inventing special-purpose infrastructure for copying only the minimal part of a pg_type tuple.
-rw-r--r--src/backend/commands/analyze.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/src/backend/commands/analyze.c b/src/backend/commands/analyze.c
index 9e5f0213803..d74e4791c92 100644
--- a/src/backend/commands/analyze.c
+++ b/src/backend/commands/analyze.c
@@ -831,12 +831,11 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
stats->attrtypmod = attr->atttypmod;
}
- typtuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(stats->attrtypid));
+ typtuple = SearchSysCacheCopy1(TYPEOID,
+ ObjectIdGetDatum(stats->attrtypid));
if (!HeapTupleIsValid(typtuple))
elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
- stats->attrtype = (Form_pg_type) palloc(sizeof(FormData_pg_type));
- memcpy(stats->attrtype, GETSTRUCT(typtuple), sizeof(FormData_pg_type));
- ReleaseSysCache(typtuple);
+ stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
stats->anl_context = anl_context;
stats->tupattnum = attnum;
@@ -865,7 +864,7 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
{
- pfree(stats->attrtype);
+ heap_freetuple(typtuple);
pfree(stats->attr);
pfree(stats);
return NULL;