diff options
author | Teodor Sigaev <teodor@sigaev.ru> | 2009-04-02 18:06:12 +0000 |
---|---|---|
committer | Teodor Sigaev <teodor@sigaev.ru> | 2009-04-02 18:06:12 +0000 |
commit | da5bfbd970fd40b89586991e9227df8e93f744b3 (patch) | |
tree | 44ed2c869dee762aee9a0db4b7014fad07a70391 | |
parent | c1b0788a9ad50fa03bfb432bb86fc0f777f16a12 (diff) | |
download | postgresql-da5bfbd970fd40b89586991e9227df8e93f744b3.tar.gz postgresql-da5bfbd970fd40b89586991e9227df8e93f744b3.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.c | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c index 08fc32e5099..02eace243d8 100644 --- a/contrib/hstore/hstore_io.c +++ b/contrib/hstore/hstore_io.c @@ -442,7 +442,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); @@ -456,8 +457,14 @@ hstore_out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(out); } - buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size + - 2 /* esc */ * (in->len - 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++) |