diff options
author | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2021-02-15 09:28:08 +0200 |
---|---|---|
committer | Heikki Linnakangas <heikki.linnakangas@iki.fi> | 2021-02-15 09:29:39 +0200 |
commit | 18cacf89b9fe5523941b57fbd01d408585e70737 (patch) | |
tree | 88af82b3ec4734e2d6c1365c48de8c02d7dc93c9 /src/backend/executor/execUtils.c | |
parent | 6c23e5ae9ee12ff1f5183573885bfaa4eb97b243 (diff) | |
download | postgresql-18cacf89b9fe5523941b57fbd01d408585e70737.tar.gz postgresql-18cacf89b9fe5523941b57fbd01d408585e70737.zip |
Make ExecGetInsertedCols() and friends more robust and improve comments.
If ExecGetInsertedCols(), ExecGetUpdatedCols() or ExecGetExtraUpdatedCols()
were called with a ResultRelInfo that's not in the range table and isn't a
partition routing target, the functions would dereference a NULL pointer,
relinfo->ri_RootResultRelInfo. Such ResultRelInfos are created when firing
RI triggers in tables that are not modified directly. None of the current
callers of these functions pass such relations, so this isn't a live bug,
but let's make them more robust.
Also update comment in ResultRelInfo; after commit 6214e2b228,
ri_RangeTableIndex is zero for ResultRelInfos created for partition tuple
routing.
Noted by Coverity. Backpatch down to v11, like commit 6214e2b228.
Reviewed-by: Tom Lane, Amit Langote
Diffstat (limited to 'src/backend/executor/execUtils.c')
-rw-r--r-- | src/backend/executor/execUtils.c | 28 |
1 files changed, 21 insertions, 7 deletions
diff --git a/src/backend/executor/execUtils.c b/src/backend/executor/execUtils.c index 57771ba78f7..fe4925f506e 100644 --- a/src/backend/executor/execUtils.c +++ b/src/backend/executor/execUtils.c @@ -1210,10 +1210,10 @@ Bitmapset * ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate) { /* - * The columns are stored in the range table entry. If this ResultRelInfo - * doesn't have an entry in the range table (i.e. if it represents a - * partition routing target), fetch the parent's RTE and map the columns - * to the order they are in the partition. + * The columns are stored in the range table entry. If this ResultRelInfo + * represents a partition routing target, and doesn't have an entry of its + * own in the range table, fetch the parent's RTE and map the columns to + * the order they are in the partition. */ if (relinfo->ri_RangeTableIndex != 0) { @@ -1221,7 +1221,7 @@ ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate) return rte->insertedCols; } - else + else if (relinfo->ri_RootResultRelInfo) { ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo; RangeTblEntry *rte = exec_rt_fetch(rootRelInfo->ri_RangeTableIndex, estate); @@ -1233,6 +1233,16 @@ ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate) else return rte->insertedCols; } + else + { + /* + * The relation isn't in the range table and it isn't a partition + * routing target. This ResultRelInfo must've been created only for + * firing triggers and the relation is not being inserted into. (See + * ExecGetTriggerResultRel.) + */ + return NULL; + } } /* Return a bitmap representing columns being updated */ @@ -1246,7 +1256,7 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate) return rte->updatedCols; } - else + else if (relinfo->ri_RootResultRelInfo) { ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo; RangeTblEntry *rte = exec_rt_fetch(rootRelInfo->ri_RangeTableIndex, estate); @@ -1258,6 +1268,8 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate) else return rte->updatedCols; } + else + return NULL; } /* Return a bitmap representing generated columns being updated */ @@ -1271,7 +1283,7 @@ ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate) return rte->extraUpdatedCols; } - else + else if (relinfo->ri_RootResultRelInfo) { ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo; RangeTblEntry *rte = exec_rt_fetch(rootRelInfo->ri_RangeTableIndex, estate); @@ -1283,6 +1295,8 @@ ExecGetExtraUpdatedCols(ResultRelInfo *relinfo, EState *estate) else return rte->extraUpdatedCols; } + else + return NULL; } /* Return columns being updated, including generated columns */ |