Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a49585

Browse files
committed
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 thereisn't, ignore anything after the last newline. This makes it a tiny bitmore robust in case the file is appended to concurrently, so that we don'treturn the last line if it hasn't been fully written yet. And this makesthe code a bit less obscure, anyway. Per Tom Lane's suggestion.Backpatch to all supported branches.
1 parent160984c commit2a49585

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

‎src/bin/pg_ctl/pg_ctl.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,14 @@ readfile(const char *path)
338338
if (fd<0)
339339
returnNULL;
340340
if (fstat(fd,&statbuf)<0)
341+
{
342+
close(fd);
341343
returnNULL;
344+
}
342345
if (statbuf.st_size==0)
343346
{
344347
/* empty file */
348+
close(fd);
345349
result= (char**)pg_malloc(sizeof(char*));
346350
*result=NULL;
347351
returnresult;
@@ -357,14 +361,17 @@ readfile(const char *path)
357361
returnNULL;
358362
}
359363

360-
/* count newlines */
364+
/*
365+
* Count newlines. We expect there to be a newline after each full line,
366+
* including one at the end of file. If there isn't a newline at the end,
367+
* any characters after the last newline will be ignored.
368+
*/
361369
nlines=0;
362-
for (i=0;i<len-1;i++)
370+
for (i=0;i<len;i++)
363371
{
364372
if (buffer[i]=='\n')
365373
nlines++;
366374
}
367-
nlines++;/* account for the last line */
368375

369376
/* set up the result buffer */
370377
result= (char**)pg_malloc((nlines+1)*sizeof(char*));
@@ -374,7 +381,7 @@ readfile(const char *path)
374381
n=0;
375382
for (i=0;i<len;i++)
376383
{
377-
if (buffer[i]=='\n'||i==len-1)
384+
if (buffer[i]=='\n')
378385
{
379386
intslen=&buffer[i]-linebegin+1;
380387
char*linebuf=pg_malloc(slen+1);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp