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

Commitb1d55dc

Browse files
committed
Fix memory leak in syslogger: logfile_rotate() would leak a copy of the
output filename if CSV logging was enabled and only one of the two possibleoutput files got rotated during a particular call (which would, in fact,typically be the case during a size-based rotation). This would amount toabout MAXPGPATH (1KB) per rotation, and it's been there since the CSVcode was put in, so it's surprising that nobody noticed it before.Per bug #5196 from Thomas Poindessous.
1 parentc742b79 commitb1d55dc

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

‎src/backend/postmaster/syslogger.c

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.52 2009/11/05 20:13:06 petere Exp $
21+
* $PostgreSQL: pgsql/src/backend/postmaster/syslogger.c,v 1.53 2009/11/19 02:45:33 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -140,7 +140,7 @@ static void open_csvlogfile(void);
140140
staticunsignedint __stdcallpipeThread(void*arg);
141141
#endif
142142
staticvoidlogfile_rotate(booltime_based_rotation,intsize_rotation_for);
143-
staticchar*logfile_getname(pg_time_ttimestamp,char*suffix);
143+
staticchar*logfile_getname(pg_time_ttimestamp,constchar*suffix);
144144
staticvoidset_next_rotation_time(void);
145145
staticvoidsigHupHandler(SIGNAL_ARGS);
146146
staticvoidsigUsr1Handler(SIGNAL_ARGS);
@@ -1016,6 +1016,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10161016
{
10171017
char*filename;
10181018
char*csvfilename=NULL;
1019+
pg_time_tfntime;
10191020
FILE*fh;
10201021

10211022
rotation_requested= false;
@@ -1026,17 +1027,12 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10261027
* file name when we don't do the rotation immediately.
10271028
*/
10281029
if (time_based_rotation)
1029-
{
1030-
filename=logfile_getname(next_rotation_time,NULL);
1031-
if (csvlogFile!=NULL)
1032-
csvfilename=logfile_getname(next_rotation_time,".csv");
1033-
}
1030+
fntime=next_rotation_time;
10341031
else
1035-
{
1036-
filename=logfile_getname(time(NULL),NULL);
1037-
if (csvlogFile!=NULL)
1038-
csvfilename=logfile_getname(time(NULL),".csv");
1039-
}
1032+
fntime=time(NULL);
1033+
filename=logfile_getname(fntime,NULL);
1034+
if (csvlogFile!=NULL)
1035+
csvfilename=logfile_getname(fntime,".csv");
10401036

10411037
/*
10421038
* Decide whether to overwrite or append. We can overwrite if (a)
@@ -1084,7 +1080,9 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
10841080
Log_RotationAge=0;
10851081
Log_RotationSize=0;
10861082
}
1087-
pfree(filename);
1083+
1084+
if (filename)
1085+
pfree(filename);
10881086
if (csvfilename)
10891087
pfree(csvfilename);
10901088
return;
@@ -1100,8 +1098,10 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11001098
#ifdefWIN32
11011099
EnterCriticalSection(&sysfileSection);
11021100
#endif
1101+
11031102
fclose(syslogFile);
11041103
syslogFile=fh;
1104+
11051105
#ifdefWIN32
11061106
LeaveCriticalSection(&sysfileSection);
11071107
#endif
@@ -1110,6 +1110,7 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11101110
if (last_file_name!=NULL)
11111111
pfree(last_file_name);
11121112
last_file_name=filename;
1113+
filename=NULL;
11131114
}
11141115

11151116
/* Same as above, but for csv file. */
@@ -1146,7 +1147,11 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11461147
Log_RotationAge=0;
11471148
Log_RotationSize=0;
11481149
}
1149-
pfree(csvfilename);
1150+
1151+
if (filename)
1152+
pfree(filename);
1153+
if (csvfilename)
1154+
pfree(csvfilename);
11501155
return;
11511156
}
11521157

@@ -1160,8 +1165,10 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11601165
#ifdefWIN32
11611166
EnterCriticalSection(&sysfileSection);
11621167
#endif
1168+
11631169
fclose(csvlogFile);
11641170
csvlogFile=fh;
1171+
11651172
#ifdefWIN32
11661173
LeaveCriticalSection(&sysfileSection);
11671174
#endif
@@ -1170,19 +1177,28 @@ logfile_rotate(bool time_based_rotation, int size_rotation_for)
11701177
if (last_csv_file_name!=NULL)
11711178
pfree(last_csv_file_name);
11721179
last_csv_file_name=csvfilename;
1180+
csvfilename=NULL;
11731181
}
11741182

1183+
if (filename)
1184+
pfree(filename);
1185+
if (csvfilename)
1186+
pfree(csvfilename);
1187+
11751188
set_next_rotation_time();
11761189
}
11771190

11781191

11791192
/*
11801193
* construct logfile name using timestamp information
11811194
*
1195+
* If suffix isn't NULL, append it to the name, replacing any ".log"
1196+
* that may be in the pattern.
1197+
*
11821198
* Result is palloc'd.
11831199
*/
11841200
staticchar*
1185-
logfile_getname(pg_time_ttimestamp,char*suffix)
1201+
logfile_getname(pg_time_ttimestamp,constchar*suffix)
11861202
{
11871203
char*filename;
11881204
intlen;
@@ -1193,7 +1209,7 @@ logfile_getname(pg_time_t timestamp, char *suffix)
11931209

11941210
len=strlen(filename);
11951211

1196-
/* treatit as a strftime pattern */
1212+
/* treatLog_filename as a strftime pattern */
11971213
pg_strftime(filename+len,MAXPGPATH-len,Log_filename,
11981214
pg_localtime(&timestamp,log_timezone));
11991215

@@ -1202,7 +1218,7 @@ logfile_getname(pg_time_t timestamp, char *suffix)
12021218
len=strlen(filename);
12031219
if (len>4&& (strcmp(filename+ (len-4),".log")==0))
12041220
len-=4;
1205-
strncpy(filename+len,suffix,MAXPGPATH-len);
1221+
strlcpy(filename+len,suffix,MAXPGPATH-len);
12061222
}
12071223

12081224
returnfilename;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp