Skip to content

Commit 3df4fa6

Browse files
committed
Fix memory allocation for output of hstore type.
Per "maosen.zhang" <maosen.zhang@alibaba-inc.com> report.
1 parent 96e218a commit 3df4fa6

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

contrib/hstore/hstore_io.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.9 2009/03/15 22:05:17 tgl Exp $
2+
* $PostgreSQL: pgsql/contrib/hstore/hstore_io.c,v 1.10 2009/04/02 17:57:05 teodor Exp $
33
*/
44
#include "postgres.h"
55

@@ -446,7 +446,8 @@ hstore_out(PG_FUNCTION_ARGS)
446446
{
447447
HStore *in = PG_GETARG_HS(0);
448448
int buflen,
449-
i;
449+
i,
450+
nnulls=0;
450451
char *out,
451452
*ptr;
452453
char *base = STRPTR(in);
@@ -460,8 +461,15 @@ hstore_out(PG_FUNCTION_ARGS)
460461
PG_RETURN_CSTRING(out);
461462
}
462463

463-
buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size +
464-
2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0));
464+
for (i = 0; i < in->size; i++)
465+
if (entries[i].valisnull)
466+
nnulls++;
467+
468+
buflen = (4 /* " */ + 2 /* => */ ) * ( in->size - nnulls ) +
469+
( 2 /* " */ + 2 /* => */ + 4 /* NULL */ ) * nnulls +
470+
2 /* , */ * ( in->size - 1 ) +
471+
2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0)) +
472+
1 /* \0 */;
465473

466474
out = ptr = palloc(buflen);
467475
for (i = 0; i < in->size; i++)

0 commit comments

Comments
 (0)