From b48ecf862b3896631660ee8d38054aded82a4f8b Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Mon, 29 Jun 2015 23:53:05 -0400
Subject: [PATCH] In bttext_abbrev_convert, move pfree to the right place.

Without this, we might access memory that's already been freed, or
leak memory if in the C locale.

Peter Geoghegan
---
 src/backend/utils/adt/varlena.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index 779729d724a..2fbbf5475ec 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -2034,13 +2034,9 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
 		}
 
 		/* Just like strcoll(), strxfrm() expects a NUL-terminated string */
-		memcpy(tss->buf1, VARDATA_ANY(authoritative), len);
+		memcpy(tss->buf1, authoritative_data, len);
 		tss->buf1[len] = '\0';
 
-		/* Don't leak memory here */
-		if (PointerGetDatum(authoritative) != original)
-			pfree(authoritative);
-
 		for (;;)
 		{
 #ifdef HAVE_LOCALE_T
@@ -2108,6 +2104,10 @@ bttext_abbrev_convert(Datum original, SortSupport ssup)
 
 	addHyperLogLog(&tss->abbr_card, hash);
 
+	/* Don't leak memory here */
+	if (PointerGetDatum(authoritative) != original)
+		pfree(authoritative);
+
 	return res;
 }
 
-- 
GitLab