mirror of
				https://github.com/postgres/postgres.git
				synced 2025-11-03 09:13:20 +03:00 
			
		
		
		
	Fix a couple of memory leaks in src/bin/pg_basebackup/
These have been introduced by 7fbe0c8, and could happen for
pg_basebackup and pg_receivewal.
Per report from Coverity for the ones in walmethods.c, I have spotted
the ones in receivelog.c after more review.
Backpatch-through: 10
			
			
This commit is contained in:
		@@ -121,6 +121,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 | 
				
			|||||||
		{
 | 
							{
 | 
				
			||||||
			pg_log_error("could not get size of write-ahead log file \"%s\": %s",
 | 
								pg_log_error("could not get size of write-ahead log file \"%s\": %s",
 | 
				
			||||||
						 fn, stream->walmethod->getlasterror());
 | 
											 fn, stream->walmethod->getlasterror());
 | 
				
			||||||
 | 
								pg_free(fn);
 | 
				
			||||||
			return false;
 | 
								return false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if (size == WalSegSz)
 | 
							if (size == WalSegSz)
 | 
				
			||||||
@@ -131,6 +132,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 | 
				
			|||||||
			{
 | 
								{
 | 
				
			||||||
				pg_log_error("could not open existing write-ahead log file \"%s\": %s",
 | 
									pg_log_error("could not open existing write-ahead log file \"%s\": %s",
 | 
				
			||||||
							 fn, stream->walmethod->getlasterror());
 | 
												 fn, stream->walmethod->getlasterror());
 | 
				
			||||||
 | 
									pg_free(fn);
 | 
				
			||||||
				return false;
 | 
									return false;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -139,11 +141,13 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 | 
				
			|||||||
			{
 | 
								{
 | 
				
			||||||
				pg_log_error("could not fsync existing write-ahead log file \"%s\": %s",
 | 
									pg_log_error("could not fsync existing write-ahead log file \"%s\": %s",
 | 
				
			||||||
							 fn, stream->walmethod->getlasterror());
 | 
												 fn, stream->walmethod->getlasterror());
 | 
				
			||||||
 | 
									pg_free(fn);
 | 
				
			||||||
				stream->walmethod->close(f, CLOSE_UNLINK);
 | 
									stream->walmethod->close(f, CLOSE_UNLINK);
 | 
				
			||||||
				return false;
 | 
									return false;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			walfile = f;
 | 
								walfile = f;
 | 
				
			||||||
 | 
								pg_free(fn);
 | 
				
			||||||
			return true;
 | 
								return true;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		if (size != 0)
 | 
							if (size != 0)
 | 
				
			||||||
@@ -155,6 +159,7 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 | 
				
			|||||||
								  "write-ahead log file \"%s\" has %d bytes, should be 0 or %d",
 | 
													  "write-ahead log file \"%s\" has %d bytes, should be 0 or %d",
 | 
				
			||||||
								  size),
 | 
													  size),
 | 
				
			||||||
						 fn, (int) size, WalSegSz);
 | 
											 fn, (int) size, WalSegSz);
 | 
				
			||||||
 | 
								pg_free(fn);
 | 
				
			||||||
			return false;
 | 
								return false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		/* File existed and was empty, so fall through and open */
 | 
							/* File existed and was empty, so fall through and open */
 | 
				
			||||||
@@ -168,9 +173,11 @@ open_walfile(StreamCtl *stream, XLogRecPtr startpoint)
 | 
				
			|||||||
	{
 | 
						{
 | 
				
			||||||
		pg_log_error("could not open write-ahead log file \"%s\": %s",
 | 
							pg_log_error("could not open write-ahead log file \"%s\": %s",
 | 
				
			||||||
					 fn, stream->walmethod->getlasterror());
 | 
										 fn, stream->walmethod->getlasterror());
 | 
				
			||||||
 | 
							pg_free(fn);
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pg_free(fn);
 | 
				
			||||||
	walfile = f;
 | 
						walfile = f;
 | 
				
			||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -95,6 +95,7 @@ dir_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
 | 
				
			|||||||
	filename = dir_get_file_name(pathname, temp_suffix);
 | 
						filename = dir_get_file_name(pathname, temp_suffix);
 | 
				
			||||||
	snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
						snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
				
			||||||
			 dir_data->basedir, filename);
 | 
								 dir_data->basedir, filename);
 | 
				
			||||||
 | 
						pg_free(filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
						/*
 | 
				
			||||||
	 * Open a file for non-compressed as well as compressed files. Tracking
 | 
						 * Open a file for non-compressed as well as compressed files. Tracking
 | 
				
			||||||
@@ -255,11 +256,13 @@ dir_close(Walfile f, WalCloseMethod method)
 | 
				
			|||||||
			filename = dir_get_file_name(df->pathname, df->temp_suffix);
 | 
								filename = dir_get_file_name(df->pathname, df->temp_suffix);
 | 
				
			||||||
			snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
								snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
				
			||||||
					 dir_data->basedir, filename);
 | 
										 dir_data->basedir, filename);
 | 
				
			||||||
 | 
								pg_free(filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			/* permanent name, so no need for the prefix */
 | 
								/* permanent name, so no need for the prefix */
 | 
				
			||||||
			filename2 = dir_get_file_name(df->pathname, NULL);
 | 
								filename2 = dir_get_file_name(df->pathname, NULL);
 | 
				
			||||||
			snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
 | 
								snprintf(tmppath2, sizeof(tmppath2), "%s/%s",
 | 
				
			||||||
					 dir_data->basedir, filename2);
 | 
										 dir_data->basedir, filename2);
 | 
				
			||||||
 | 
								pg_free(filename2);
 | 
				
			||||||
			r = durable_rename(tmppath, tmppath2);
 | 
								r = durable_rename(tmppath, tmppath2);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else if (method == CLOSE_UNLINK)
 | 
							else if (method == CLOSE_UNLINK)
 | 
				
			||||||
@@ -270,6 +273,7 @@ dir_close(Walfile f, WalCloseMethod method)
 | 
				
			|||||||
			filename = dir_get_file_name(df->pathname, df->temp_suffix);
 | 
								filename = dir_get_file_name(df->pathname, df->temp_suffix);
 | 
				
			||||||
			snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
								snprintf(tmppath, sizeof(tmppath), "%s/%s",
 | 
				
			||||||
					 dir_data->basedir, filename);
 | 
										 dir_data->basedir, filename);
 | 
				
			||||||
 | 
								pg_free(filename);
 | 
				
			||||||
			r = unlink(tmppath);
 | 
								r = unlink(tmppath);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		else
 | 
							else
 | 
				
			||||||
@@ -626,11 +630,14 @@ tar_open_for_write(const char *pathname, const char *temp_suffix, size_t pad_to_
 | 
				
			|||||||
	if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
 | 
						if (tarCreateHeader(tar_data->currentfile->header, tmppath, NULL, 0, S_IRUSR | S_IWUSR, 0, 0, time(NULL)) != TAR_OK)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		pg_free(tar_data->currentfile);
 | 
							pg_free(tar_data->currentfile);
 | 
				
			||||||
 | 
							pg_free(tmppath);
 | 
				
			||||||
		tar_data->currentfile = NULL;
 | 
							tar_data->currentfile = NULL;
 | 
				
			||||||
		tar_set_error("could not create tar header");
 | 
							tar_set_error("could not create tar header");
 | 
				
			||||||
		return NULL;
 | 
							return NULL;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						pg_free(tmppath);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef HAVE_LIBZ
 | 
					#ifdef HAVE_LIBZ
 | 
				
			||||||
	if (tar_data->compression)
 | 
						if (tar_data->compression)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -53,8 +53,8 @@ struct WalWriteMethod
 | 
				
			|||||||
	ssize_t		(*get_file_size) (const char *pathname);
 | 
						ssize_t		(*get_file_size) (const char *pathname);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/*
 | 
						/*
 | 
				
			||||||
	 * Return the name of the current file to work on, without the base
 | 
						 * Return the name of the current file to work on in pg_malloc()'d string,
 | 
				
			||||||
	 * directory.  This is useful for logging.
 | 
						 * without the base directory.  This is useful for logging.
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	char	   *(*get_file_name) (const char *pathname, const char *temp_suffix);
 | 
						char	   *(*get_file_name) (const char *pathname, const char *temp_suffix);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user