Обсуждение: libpq leaks memory for SSL connections

Поиск
Список
Период
Сортировка

libpq leaks memory for SSL connections

От
Timo Sirainen
Дата:
I noticed with valgrind how libpq is leaking memory:

at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
by 0x5F645C8: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
by 0x5E94EEF: BIO_meth_new (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
by 0x65181EB: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
by 0x651950C: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
by 0x650140F: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)

Debian doesn't have debuginfo packages for libpq, but looking at the code I think it's pretty clear where the bug is:

my_SSL_set_fd() calls my_BIO_s_socket() which allocates memory with BIO_meth_new(), but nothing ever frees it. Note
thatOpenSSL's BIO_s_socket() returns a const pointer to statically allocated memory. 




Re: libpq leaks memory for SSL connections

От
Tom Lane
Дата:
Timo Sirainen <timo@sirainen.com> writes:
> I noticed with valgrind how libpq is leaking memory:
> at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
> by 0x5F645C8: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
> by 0x5E94EEF: BIO_meth_new (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
> by 0x65181EB: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
> by 0x651950C: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
> by 0x650140F: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)

I see no leak here.  The struct is allocated once and kept for possible
re-use by future connections.  valgrind concurs, saying it's "still
reachable".

            regards, tom lane



Re: libpq leaks memory for SSL connections

От
Timo Sirainen
Дата:
On 1. Oct 2021, at 21.46, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> Timo Sirainen <timo@sirainen.com> writes:
>> I noticed with valgrind how libpq is leaking memory:
>> at 0x483577F: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
>> by 0x5F645C8: CRYPTO_zalloc (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
>> by 0x5E94EEF: BIO_meth_new (in /usr/lib/x86_64-linux-gnu/libcrypto.so.1.1)
>> by 0x65181EB: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
>> by 0x651950C: ??? (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
>> by 0x650140F: PQconnectPoll (in /usr/lib/x86_64-linux-gnu/libpq.so.5.11)
>
> I see no leak here.  The struct is allocated once and kept for possible
> re-use by future connections.  valgrind concurs, saying it's "still
> reachable".

Oh, right, somehow I missed it didn't allocate new memory every time. And for some reason valgrind tells me it is
"definitelylost". It would be nice if there was some PQglobalDeinit() function that could be called to free it to avoid
havingto add valgrind suppression, but I guess that's more of a feature request.