Skip to content

Commit 780a342

Browse files
committed
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.
1 parent 6f94280 commit 780a342

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

src/backend/commands/analyze.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -844,12 +844,11 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
844844
stats->attrtypmod = attr->atttypmod;
845845
}
846846

847-
typtuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(stats->attrtypid));
847+
typtuple = SearchSysCacheCopy1(TYPEOID,
848+
ObjectIdGetDatum(stats->attrtypid));
848849
if (!HeapTupleIsValid(typtuple))
849850
elog(ERROR, "cache lookup failed for type %u", stats->attrtypid);
850-
stats->attrtype = (Form_pg_type) palloc(sizeof(FormData_pg_type));
851-
memcpy(stats->attrtype, GETSTRUCT(typtuple), sizeof(FormData_pg_type));
852-
ReleaseSysCache(typtuple);
851+
stats->attrtype = (Form_pg_type) GETSTRUCT(typtuple);
853852
stats->anl_context = anl_context;
854853
stats->tupattnum = attnum;
855854

@@ -878,7 +877,7 @@ examine_attribute(Relation onerel, int attnum, Node *index_expr)
878877

879878
if (!ok || stats->compute_stats == NULL || stats->minrows <= 0)
880879
{
881-
pfree(stats->attrtype);
880+
heap_freetuple(typtuple);
882881
pfree(stats->attr);
883882
pfree(stats);
884883
return NULL;

0 commit comments

Comments
 (0)