From 52cdbdc42c23e4f9ae1e62e95bd85d11c030d3f6 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Tue, 25 Jun 2019 11:15:38 +0900
Subject: [PATCH] Fix thinkos in LookupFuncName() for function name lookups

This could trigger valgrind failures when doing ambiguous function name
lookups when no arguments are provided by the caller.  The problem has
been introduced in aefeb68, so backpatch to v10.  HEAD is fine thanks to
the refactoring done in bfb456c1.

Reported-by: Alexander Lakhin
Author: Alexander Lakhin, Michael Paquier
Discussion: https://postgr.es/m/3d068be5-f617-a5ee-99f6-458a407bfd65@gmail.com
Backpatch-through: 10
---
 src/backend/parser/parse_func.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c
index 44257154b8..141007d5a4 100644
--- a/src/backend/parser/parse_func.c
+++ b/src/backend/parser/parse_func.c
@@ -2059,9 +2059,10 @@ LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
 							 errmsg("function name \"%s\" is not unique",
 									NameListToString(funcname)),
 							 errhint("Specify the argument list to select the function unambiguously.")));
+				return InvalidOid;
 			}
-			else
-				return clist->oid;
+			/* Otherwise return the match */
+			return clist->oid;
 		}
 		else
 		{
@@ -2070,9 +2071,14 @@ LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
 						(errcode(ERRCODE_UNDEFINED_FUNCTION),
 						 errmsg("could not find a function named \"%s\"",
 								NameListToString(funcname))));
+			return InvalidOid;
 		}
 	}
 
+	/*
+	 * Otherwise, look for a match to the arg types.  FuncnameGetCandidates
+	 * has ensured that there's at most one match in the returned list.
+	 */
 	while (clist)
 	{
 		if (memcmp(argtypes, clist->args, nargs * sizeof(Oid)) == 0)
-- 
2.40.0