Skip to content

Commit fd602f2

Browse files
committed
Clean up impenetrable logic in pg_basebackup/receivelog.c.
Coverity complained about possible double free of HandleCopyStream's "copybuf". AFAICS it's mistaken, but it is easy to see why it's confused, because management of that buffer is impossibly confusing. It's unreasonable that HandleEndOfCopyStream frees the buffer in some cases but not others, updates the caller's state for that in no case, and has not a single comment about how complicated that makes things. Let's put all the responsibility for freeing copybuf in the actual owner of that variable, HandleCopyStream. This results in one more PQfreemem call than before, but the logic is far easier to follow, both for humans and machines. Since this isn't (quite) actually broken, no back-patch.
1 parent fcd77a6 commit fd602f2

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/bin/pg_basebackup/receivelog.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,10 @@ HandleCopyStream(PGconn *conn, StreamCtl *stream,
803803
sleeptime = CalculateCopyStreamSleeptime(now, stream->standby_message_timeout,
804804
last_status);
805805

806+
/* Done with any prior message */
807+
PQfreemem(copybuf);
808+
copybuf = NULL;
809+
806810
r = CopyStreamReceive(conn, sleeptime, stream->stop_socket, &copybuf);
807811
while (r != 0)
808812
{
@@ -814,8 +818,8 @@ HandleCopyStream(PGconn *conn, StreamCtl *stream,
814818

815819
if (res == NULL)
816820
goto error;
817-
else
818-
return res;
821+
PQfreemem(copybuf);
822+
return res;
819823
}
820824

821825
/* Check the message type. */
@@ -844,6 +848,10 @@ HandleCopyStream(PGconn *conn, StreamCtl *stream,
844848
goto error;
845849
}
846850

851+
/* Done with that message */
852+
PQfreemem(copybuf);
853+
copybuf = NULL;
854+
847855
/*
848856
* Process the received data, and any subsequent data we can read
849857
* without blocking.
@@ -920,8 +928,8 @@ CopyStreamPoll(PGconn *conn, long timeout_ms, pgsocket stop_socket)
920928
* maximum of 'timeout' ms.
921929
*
922930
* If data was received, returns the length of the data. *buffer is set to
923-
* point to a buffer holding the received message. The buffer is only valid
924-
* until the next CopyStreamReceive call.
931+
* point to a buffer holding the received message. The caller must eventually
932+
* free the buffer with PQfreemem().
925933
*
926934
* Returns 0 if no data was available within timeout, or if wait was
927935
* interrupted by signal or stop_socket input.
@@ -934,8 +942,8 @@ CopyStreamReceive(PGconn *conn, long timeout, pgsocket stop_socket,
934942
char *copybuf = NULL;
935943
int rawlen;
936944

937-
PQfreemem(*buffer);
938-
*buffer = NULL;
945+
/* Caller should have cleared any prior buffer */
946+
Assert(*buffer == NULL);
939947

940948
/* Try to receive a CopyData message */
941949
rawlen = PQgetCopyData(conn, &copybuf, 1);
@@ -1198,7 +1206,6 @@ HandleEndOfCopyStream(PGconn *conn, StreamCtl *stream, char *copybuf,
11981206
}
11991207
still_sending = false;
12001208
}
1201-
PQfreemem(copybuf);
12021209
*stoppos = blockpos;
12031210
return res;
12041211
}

0 commit comments

Comments
 (0)