Re: memory leaking ? - Mailing list pgsql-general

From Tom Lane
Subject Re: memory leaking ?
Date
Msg-id 8787.1401302268@sss.pgh.pa.us
Whole thread Raw
In response to memory leaking ?  (Zhemin Zhou <Zhemin.Zhou@warwick.ac.uk>)
List pgsql-general
Zhemin Zhou <Zhemin.Zhou@warwick.ac.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


pgsql-general by date:

Previous
From: Marc Brazeau
Date:
Subject: log_collector & sysout on windows
Next
From: Shaun Thomas
Date:
Subject: Re: Conversion from CHAR HEX