提交 2a49585e 编写于 作者: H Heikki Linnakangas

Further tweaking of the readfile() function in pg_ctl.

Don't leak a file descriptor if the file is empty or we can't read its size.

Expect there to be a newline at the end of the last line, too. If there
isn't, ignore anything after the last newline. This makes it a tiny bit
more robust in case the file is appended to concurrently, so that we don't
return the last line if it hasn't been fully written yet. And this makes
the code a bit less obscure, anyway. Per Tom Lane's suggestion.

Backpatch to all supported branches.
上级 160984c8
...@@ -338,10 +338,14 @@ readfile(const char *path) ...@@ -338,10 +338,14 @@ readfile(const char *path)
if (fd < 0) if (fd < 0)
return NULL; return NULL;
if (fstat(fd, &statbuf) < 0) if (fstat(fd, &statbuf) < 0)
{
close(fd);
return NULL; return NULL;
}
if (statbuf.st_size == 0) if (statbuf.st_size == 0)
{ {
/* empty file */ /* empty file */
close(fd);
result = (char **) pg_malloc(sizeof(char *)); result = (char **) pg_malloc(sizeof(char *));
*result = NULL; *result = NULL;
return result; return result;
...@@ -357,14 +361,17 @@ readfile(const char *path) ...@@ -357,14 +361,17 @@ readfile(const char *path)
return NULL; return NULL;
} }
/* count newlines */ /*
* Count newlines. We expect there to be a newline after each full line,
* including one at the end of file. If there isn't a newline at the end,
* any characters after the last newline will be ignored.
*/
nlines = 0; nlines = 0;
for (i = 0; i < len - 1; i++) for (i = 0; i < len; i++)
{ {
if (buffer[i] == '\n') if (buffer[i] == '\n')
nlines++; nlines++;
} }
nlines++; /* account for the last line */
/* set up the result buffer */ /* set up the result buffer */
result = (char **) pg_malloc((nlines + 1) * sizeof(char *)); result = (char **) pg_malloc((nlines + 1) * sizeof(char *));
...@@ -374,7 +381,7 @@ readfile(const char *path) ...@@ -374,7 +381,7 @@ readfile(const char *path)
n = 0; n = 0;
for (i = 0; i < len; i++) for (i = 0; i < len; i++)
{ {
if (buffer[i] == '\n' || i == len - 1) if (buffer[i] == '\n')
{ {
int slen = &buffer[i] - linebegin + 1; int slen = &buffer[i] - linebegin + 1;
char *linebuf = pg_malloc(slen + 1); char *linebuf = pg_malloc(slen + 1);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册