*** 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;
  	}
  
