libpq: fix unlikely memory leak

Lists: pgsql-patches
From: Neil Conway <neilc(at)samurai(dot)com>
To: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: libpq: fix unlikely memory leak
Date: 2005-06-29 08:25:50
Message-ID: 42C25B0E.1000400@samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

The attached patch fixes a theoretical memory leak in libpq: if the
second malloc() fails due to OOM, the memory returned by the first
(successful) malloc() will be leaked.

Barring any objections I'll apply this tomorrow.

Per report from EnterpriseDB's Coverity analysis.

-Neil

Attachment Content-Type Size
libpq_send_passwd_mem_leak-1.patch text/x-patch 1.8 KB

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: libpq: fix unlikely memory leak
Date: 2005-06-29 14:48:21
Message-ID: 9344.1120056501@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Neil Conway <neilc(at)samurai(dot)com> writes:
> The attached patch fixes a theoretical memory leak in libpq: if the
> second malloc() fails due to OOM, the memory returned by the first
> (successful) malloc() will be leaked.

Since both allocations are only transient within this routine, there's
a simpler more effective solution, which is to only do one malloc in
the first place:

char *crypt_pwd2;

/* need enough space for 2 MD5 hashes */
if (!(crypt_pwd = malloc(2 * (MD5_PASSWD_LEN + 1))))
{
fprintf(stderr, libpq_gettext("out of memory\n"));
return STATUS_ERROR;
}
crypt_pwd2 = crypt_pwd + (MD5_PASSWD_LEN + 1);

and drop the free(crypt_pwd2) calls.

regards, tom lane


From: Neil Conway <neilc(at)samurai(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: Re: libpq: fix unlikely memory leak
Date: 2005-06-30 02:00:25
Message-ID: 42C35239.7060703@samurai.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-patches

Tom Lane wrote:
> Since both allocations are only transient within this routine, there's
> a simpler more effective solution, which is to only do one malloc in
> the first place

Yeah, true. Attached is a revised patch -- committed to HEAD.

-Neil

Attachment Content-Type Size
libpq_send_passwd_mem_leak-2.patch text/x-patch 1.8 KB