Commit 3df4fa69 authored by Teodor Sigaev's avatar Teodor Sigaev

Fix memory allocation for output of hstore type.

Per "maosen.zhang" <maosen.zhang@alibaba-inc.com> report.
parent 96e218a0
/* /*
* $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" #include "postgres.h"
...@@ -446,7 +446,8 @@ hstore_out(PG_FUNCTION_ARGS) ...@@ -446,7 +446,8 @@ hstore_out(PG_FUNCTION_ARGS)
{ {
HStore *in = PG_GETARG_HS(0); HStore *in = PG_GETARG_HS(0);
int buflen, int buflen,
i; i,
nnulls=0;
char *out, char *out,
*ptr; *ptr;
char *base = STRPTR(in); char *base = STRPTR(in);
...@@ -460,8 +461,15 @@ hstore_out(PG_FUNCTION_ARGS) ...@@ -460,8 +461,15 @@ hstore_out(PG_FUNCTION_ARGS)
PG_RETURN_CSTRING(out); PG_RETURN_CSTRING(out);
} }
buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size + for (i = 0; i < in->size; i++)
2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0)); 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); out = ptr = palloc(buflen);
for (i = 0; i < in->size; i++) for (i = 0; i < in->size; i++)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment