From c080b223a7a3991524a5287416a0ad756c15a098 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Tue, 20 Dec 2016 19:22:02 -0500
Subject: [PATCH] Fix minor oversights in nodeAgg.c.

aggstate->evalproj is always set up by ExecInitAgg, so there's no
need to test.  Doing so led Coverity to think that we might be
intending "slot" to be possibly NULL here, and it quite properly
complained that the rest of combine_aggregates() wasn't prepared
for that.

Also fix a couple of obvious thinkos in Asserts checking that
"inputoff" isn't past the end of the slot.

Errors introduced in commit 8ed3f11bb, so no need for back-patch.
---
 src/backend/executor/nodeAgg.c | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 781938e2bb4..ad5edbad29e 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -932,7 +932,8 @@ advance_aggregates(AggState *aggstate, AggStatePerGroup pergroup)
 
 			/* Load values into fcinfo */
 			/* Start from 1, since the 0th arg will be the transition value */
-			Assert(slot->tts_nvalid >= numTransInputs);
+			Assert(slot->tts_nvalid >= (numTransInputs + inputoff));
+
 			for (i = 0; i < numTransInputs; i++)
 			{
 				fcinfo->arg[i + 1] = slot->tts_values[i + inputoff];
@@ -963,14 +964,13 @@ combine_aggregates(AggState *aggstate, AggStatePerGroup pergroup)
 {
 	int			transno;
 	int			numTrans = aggstate->numtrans;
-	TupleTableSlot *slot = NULL;
+	TupleTableSlot *slot;
 
 	/* combine not supported with grouping sets */
 	Assert(aggstate->phase->numsets == 0);
 
 	/* compute input for all aggregates */
-	if (aggstate->evalproj)
-		slot = ExecProject(aggstate->evalproj, NULL);
+	slot = ExecProject(aggstate->evalproj, NULL);
 
 	for (transno = 0; transno < numTrans; transno++)
 	{
@@ -979,8 +979,7 @@ combine_aggregates(AggState *aggstate, AggStatePerGroup pergroup)
 		FunctionCallInfo fcinfo = &pertrans->transfn_fcinfo;
 		int			inputoff = pertrans->inputoff;
 
-		Assert(slot->tts_nvalid >= 1);
-		Assert(slot->tts_nvalid + inputoff >= 1);
+		Assert(slot->tts_nvalid > inputoff);
 
 		/*
 		 * deserialfn_oid will be set if we must deserialize the input state
-- 
GitLab