Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f1d0e64

Browse files
committed
Prevent file descriptor leak from failed COPY.
1 parent 88800aa commit f1d0e64

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

src/backend/commands/copy.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.56 1998/08/29 05:27:15 momjian Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.57 1998/08/29 18:19:59 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -108,12 +108,21 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
108108
the class.
109109
----------------------------------------------------------------------------*/
110110

111-
FILE *fp;
111+
static FILE *fp; /* static for cleanup */
112+
static bool file_opened = false; /* static for cleanup */
112113
Relation rel;
113114
extern char *UserName; /* defined in global.c */
114115
const AclMode required_access = from ? ACL_WR : ACL_RD;
115116
int result;
116117

118+
/*
119+
* Close previous file opened for COPY but failed with elog().
120+
* There should be a better way, but would not be modular.
121+
* Prevents file descriptor leak. bjm 1998/08/29
122+
*/
123+
if (file_opened)
124+
FreeFile(fp);
125+
117126
rel = heap_openr(relname);
118127
if (rel == NULL)
119128
elog(ERROR, "COPY command failed. Class %s "
@@ -146,14 +155,13 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
146155
}
147156
else
148157
{
149-
/* if we elog() out, the file stays open */
150158
fp = AllocateFile(filename, "r");
151159
if (fp == NULL)
152160
elog(ERROR, "COPY command, running in backend with "
153161
"effective uid %d, could not open file '%s' for "
154162
"reading. Errno = %s (%d).",
155163
geteuid(), filename, strerror(errno), errno);
156-
/* Above should not return */
164+
file_opened = true;
157165
}
158166
CopyFrom(rel, binary, oids, fp, delim);
159167
}
@@ -174,20 +182,22 @@ DoCopy(char *relname, bool binary, bool oids, bool from, bool pipe,
174182
mode_t oumask; /* Pre-existing umask value */
175183

176184
oumask = umask((mode_t) 0);
177-
/* if we elog() out, the file stays open */
178185
fp = AllocateFile(filename, "w");
179186
umask(oumask);
180187
if (fp == NULL)
181188
elog(ERROR, "COPY command, running in backend with "
182189
"effective uid %d, could not open file '%s' for "
183190
"writing. Errno = %s (%d).",
184191
geteuid(), filename, strerror(errno), errno);
185-
/* Above should not return */
192+
file_opened = true;
186193
}
187194
CopyTo(rel, binary, oids, fp, delim);
188195
}
189196
if (!pipe)
197+
{
190198
FreeFile(fp);
199+
file_opened = false;
200+
}
191201
else if (!from && !binary)
192202
{
193203
fputs("\\.\n", fp);

0 commit comments

Comments
 (0)