postgres.git / summary / log / commit / refs
commit a1e9a25dc33e791e2557cfad4c2c2c4035bfee63
Author: Teodor Sigaev <teodor@sigaev.ru>
Date: Thu Apr 02 17:57:59 2009 +0000
First released in: 8.4.0, 8.2.14, 8.3.8
Fix memory allocation for output of hstore type.
Per "maosen.zhang" <maosen.zhang@alibaba-inc.com> report.
contrib/hstore/hstore_io.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index 9e6acd18727..99f1eaa129b 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -440,7 +440,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);
@@ -454,8 +455,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++)
[parent: e7a4b97917b6]