Re: memory leaking ?

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Zhemin Zhou <Zhemin(dot)Zhou(at)warwick(dot)ac(dot)uk>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: memory leaking ?
Date: 2014-05-28 18:37:48
Message-ID: 8787.1401302268@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Zhemin Zhou <Zhemin(dot)Zhou(at)warwick(dot)ac(dot)uk> writes:
> We met a problem after running the website for one week. We used a
> function to convert and save binary files into the database (as bytea).
> This function worked well in the old version but sometimes makes the new
> version of postgres crash. This random crash is not file specific.

AFAICT, it's pure luck that it didn't crash the older system too. You're
allocating the output buffer too small, at least for cases where "size"
isn't a multiple of 3:

> bytea *result = (bytea *)
> palloc(VARHDRSZ+sizeof(char)*(4*(size)/3+15));

For example, if size = 2, 4*2/3 is only 2, but the loop will write 4 bytes
of data. So the function sometimes clobbers bytes beyond what it
allocated, which unsurprisingly corrupts malloc's data structures.
You need to round up not truncate in this division.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Shaun Thomas 2014-05-28 19:11:13 Re: Conversion from CHAR HEX
Previous Message Marc Brazeau 2014-05-28 17:30:35 log_collector & sysout on windows