aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeodor Sigaev <teodor@sigaev.ru>2009-04-02 17:57:05 +0000
committerTeodor Sigaev <teodor@sigaev.ru>2009-04-02 17:57:05 +0000
commit3df4fa6968e6f71d137d690171e3d4cd07bdf206 (patch)
treefc308a9c9c7b7cad65afb7cb9babc3dc36d6b5b9
parent96e218a04975739609a209dd2eddd94d7792f2f4 (diff)
downloadpostgresql-3df4fa6968e6f71d137d690171e3d4cd07bdf206.tar.gz
postgresql-3df4fa6968e6f71d137d690171e3d4cd07bdf206.zip
Fix memory allocation for output of hstore type.
Per "maosen.zhang" <maosen.zhang@alibaba-inc.com> report.
-rw-r--r--contrib/hstore/hstore_io.c16
1 files changed, 12 insertions, 4 deletions
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 4786d7b4476..140336a2e60 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -1,5 +1,5 @@
/*
- * $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.9 2009/03/15 22:05:17 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.10 2009/04/02 17:57:05 teodor Exp $
*/
#include "postgres.h"
@@ -446,7 +446,8 @@ hstore_out(PG_FUNCTION_ARGS)
{
HStore *in = PG_GETARG_HS(0);
int buflen,
- i;
+ i,
+ nnulls=0;
char *out,
*ptr;
char *base = STRPTR(in);
@@ -460,8 +461,15 @@ hstore_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(out);
}
- buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size +
- 2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0));
+ for (i = 0; i < in->size; i++)
+ if (entries[i].valisnull)
+ nnulls++;
+
+ buflen = (4 /* " */ + 2 /* => */ ) * ( in->size - nnulls ) +
+ ( 2 /* " */ + 2 /* => */ + 4 /* NULL */ ) * nnulls +
+ 2 /* , */ * ( in->size - 1 ) +
+ 2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0)) +
+ 1 /* \0 */;
out = ptr = palloc(buflen);
for (i = 0; i < in->size; i++)