From b5217fd138f35fb5bf70ad8741ebed5330457891 Mon Sep 17 00:00:00 2001
From: Alexander Kuznetsov <kuznetsovam@altlinux.org>
Date: Thu, 10 Oct 2024 17:38:10 +0300
Subject: [PATCH] Check for tuplestorestate nullness before dereferencing

tuplestorestate can be NULL when calculating eof_tuplestore,
where tuplestorestate is dereferenced by tuplestore_gettuple().
Add check for nullness before dereferencing.

Found by ALT Linux Team with Svace.
---
 src/backend/executor/nodeMaterial.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/backend/executor/nodeMaterial.c b/src/backend/executor/nodeMaterial.c
index 22e1787fbd..5bc8561f3a 100644
--- a/src/backend/executor/nodeMaterial.c
+++ b/src/backend/executor/nodeMaterial.c
@@ -95,7 +95,7 @@ ExecMaterial(PlanState *pstate)
 			 * to return the one before that, if possible. So do an extra
 			 * fetch.
 			 */
-			if (!tuplestore_advance(tuplestorestate, forward))
+			if (tuplestorestate == NULL || !tuplestore_advance(tuplestorestate, forward))
 				return NULL;	/* the tuplestore must be empty */
 		}
 		eof_tuplestore = false;
-- 
2.42.2

