From cd55aa2e007da2dc2f4e014062c1722bbff59739 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Thu, 16 Sep 2010 02:54:01 +0000
Subject: [PATCH] Fix two new-in-9.0 bugs in hstore.

There was an incorrect Assert in hstoreValidOldFormat(), which would cause
immediate core dumps when attempting to work with pre-9.0 hstore data,
but of course only in an assert-enabled build.

Also, ghstore_decompress() incorrectly applied DatumGetHStoreP() to a datum
that wasn't actually an hstore, but rather a ghstore (ie, a gist signature
bitstring).  That used to be harmless, but could now result in misbehavior
if the hstore format conversion code happened to trigger.  In reality,
since ghstore is not marked toastable (and doesn't need to be), this
function is useless anyway; we can lobotomize it down to returning the
passed-in pointer.

Both bugs found by Andrew Gierth, though this isn't exactly his proposed
patch.
---
 contrib/hstore/hstore_compat.c |  5 +++--
 contrib/hstore/hstore_gist.c   | 26 ++++++--------------------
 2 files changed, 9 insertions(+), 22 deletions(-)

diff --git a/contrib/hstore/hstore_compat.c b/contrib/hstore/hstore_compat.c
index 033d945f9ca..ca2777cd8d8 100644
--- a/contrib/hstore/hstore_compat.c
+++ b/contrib/hstore/hstore_compat.c
@@ -1,5 +1,5 @@
 /*
- * $PostgreSQL: pgsql/contrib/hstore/hstore_compat.c,v 1.2 2010/02/26 02:00:32 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/hstore/hstore_compat.c,v 1.3 2010/09/16 02:54:01 tgl Exp $
  *
  * Notes on old/new hstore format disambiguation.
  *
@@ -180,7 +180,8 @@ hstoreValidOldFormat(HStore *hs)
 	if (hs->size_ & HS_FLAG_NEWVERSION)
 		return 0;
 
-	Assert(sizeof(HOldEntry) == sizeof(HEntry));
+	/* New format uses an HEntry for key and another for value */
+	Assert(sizeof(HOldEntry) == (2 * sizeof(HEntry)));
 
 	if (count == 0)
 		return 2;
diff --git a/contrib/hstore/hstore_gist.c b/contrib/hstore/hstore_gist.c
index db58fb62ddf..6fe1860f3bb 100644
--- a/contrib/hstore/hstore_gist.c
+++ b/contrib/hstore/hstore_gist.c
@@ -1,5 +1,5 @@
 /*
- * $PostgreSQL: pgsql/contrib/hstore/hstore_gist.c,v 1.12 2010/02/26 02:00:32 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/hstore/hstore_gist.c,v 1.13 2010/09/16 02:54:01 tgl Exp $
  */
 #include "postgres.h"
 
@@ -168,28 +168,14 @@ ghstore_compress(PG_FUNCTION_ARGS)
 	PG_RETURN_POINTER(retval);
 }
 
+/*
+ * Since type ghstore isn't toastable (and doesn't need to be),
+ * this function can be a no-op.
+ */
 Datum
 ghstore_decompress(PG_FUNCTION_ARGS)
 {
-	GISTENTRY  *entry = (GISTENTRY *) PG_GETARG_POINTER(0);
-	GISTENTRY  *retval;
-	HStore	   *key;
-
-	key = DatumGetHStoreP(entry->key);
-
-	if (key != (HStore *) DatumGetPointer(entry->key))
-	{
-		/* need to pass back the decompressed item */
-		retval = palloc(sizeof(GISTENTRY));
-		gistentryinit(*retval, PointerGetDatum(key),
-					  entry->rel, entry->page, entry->offset, entry->leafkey);
-		PG_RETURN_POINTER(retval);
-	}
-	else
-	{
-		/* we can return the entry as-is */
-		PG_RETURN_POINTER(entry);
-	}
+	PG_RETURN_POINTER(PG_GETARG_POINTER(0));
 }
 
 Datum
-- 
GitLab