Memory leak in receivelog.c when receiving stream

Lists: pgsql-hackers
From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Memory leak in receivelog.c when receiving stream
Date: 2015-01-13 08:18:42
Message-ID: CAB7nPqQVwvZzeQTp9z7SmaizZ8MXgw9bLOqK--6xa3bTx7RuGA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi all,

receivelog.c is leaking memory in CopyStreamReceive when receiving COPY data.
The issue has been spotted by coverity. The patch attached fixes the
problem, and contains as well improved comments.
Regards,
--
Michael

Attachment Content-Type Size
20150113_receivelog_leak_fix.patch text/x-diff 872 bytes

From: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
To: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>, PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Memory leak in receivelog.c when receiving stream
Date: 2015-01-13 08:45:30
Message-ID: 54B4DB2A.2000104@vmware.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On 01/13/2015 10:18 AM, Michael Paquier wrote:
> receivelog.c is leaking memory in CopyStreamReceive when receiving COPY data.
> The issue has been spotted by coverity. The patch attached fixes the
> problem, and contains as well improved comments.
>
> *** a/src/bin/pg_basebackup/receivelog.c
> --- b/src/bin/pg_basebackup/receivelog.c
> ***************
> *** 1035,1046 **** CopyStreamReceive(PGconn *conn, long timeout, char **buffer)
> if (rawlen == 0)
> return 0;
> }
> ! if (rawlen == -1) /* end-of-streaming or error */
> return -2;
> if (rawlen == -2)
> {
> fprintf(stderr, _("%s: could not read COPY data: %s"),
> progname, PQerrorMessage(conn));
> return -1;
> }
>
> --- 1035,1056 ----
> if (rawlen == 0)
> return 0;
> }
> !
> ! /* end-of-streaming or error */
> ! if (rawlen == -1)
> ! {
> ! if (copybuf != NULL)
> ! PQfreemem(copybuf);
> return -2;
> + }
> +
> + /* failure when reading copy stream */
> if (rawlen == -2)
> {
> fprintf(stderr, _("%s: could not read COPY data: %s"),
> progname, PQerrorMessage(conn));
> + if (copybuf != NULL)
> + PQfreemem(copybuf);
> return -1;
> }

This looks like a false positive to me. PQgetCopyData() will only return
a buffer if its return value is > 0

- Heikki


From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>
Cc: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Memory leak in receivelog.c when receiving stream
Date: 2015-01-13 11:14:43
Message-ID: CAB7nPqSskA635K-2HzDUAfoUBUegjrrEzfYuGhYp3oH0MOKShQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Tue, Jan 13, 2015 at 5:45 PM, Heikki Linnakangas
<hlinnakangas(at)vmware(dot)com> wrote:
> This looks like a false positive to me. PQgetCopyData() will only return a
> buffer if its return value is > 0
Right. Sorry for the noise.
--
Michael