Re: [COMMITTERS] pgsql: Clean up jsonb code.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Alexander Korotkov <aekorotkov(at)gmail(dot)com>
Cc: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, Peter Geoghegan <pg(at)heroku(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Oleg Bartunov <obartunov(at)gmail(dot)com>
Subject: Re: [COMMITTERS] pgsql: Clean up jsonb code.
Date: 2014-05-09 21:54:06
Message-ID: 6516.1399672446@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers pgsql-hackers

Alexander Korotkov <aekorotkov(at)gmail(dot)com> writes:
> With current head I can't load delicious dataset into jsonb format. I got
> segfault. It looks like memory corruption.

The proximate cause of this seems to be that reserveFromBuffer() fails
to consider the possibility that it needs to more-than-double the
current buffer size. This change makes the crash go away for me:

diff --git a/src/backend/utils/adt/jsonb_util.c b/src/backend/utils/adt/jsonb_util.c
index 832a08d..0c4af04 100644
*** a/src/backend/utils/adt/jsonb_util.c
--- b/src/backend/utils/adt/jsonb_util.c
*************** reserveFromBuffer(convertState *buffer,
*** 1186,1192 ****
/* Make more room if needed */
if (buffer->len + len > buffer->allocatedsz)
{
! buffer->allocatedsz *= 2;
buffer->buffer = repalloc(buffer->buffer, buffer->allocatedsz);
}

--- 1186,1195 ----
/* Make more room if needed */
if (buffer->len + len > buffer->allocatedsz)
{
! do
! {
! buffer->allocatedsz *= 2;
! } while (buffer->len + len > buffer->allocatedsz);
buffer->buffer = repalloc(buffer->buffer, buffer->allocatedsz);
}

However, what it looks to me like we've got here is a very bad
reimplementation of StringInfo buffers. There is for example no
integer-overflow checking here. Rather than try to bring this code
up to speed, I think we should rip it out and use StringInfo.

regards, tom lane

In response to

Responses

Browse pgsql-committers by date

  From Date Subject
Next Message Peter Geoghegan 2014-05-09 22:23:57 Re: [COMMITTERS] pgsql: Clean up jsonb code.
Previous Message Tom Lane 2014-05-09 20:33:32 pgsql: Improve user-facing JSON documentation.

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Geoghegan 2014-05-09 22:23:57 Re: [COMMITTERS] pgsql: Clean up jsonb code.
Previous Message Tom Lane 2014-05-09 21:24:46 Re: test_shm_mq failing on anole (was: Sending out a request for more buildfarm animals?)