Skip to content

Commit aa6b7b7

Browse files
committed
Fix saving and restoring umask
In two cases, we set a different umask for some piece of code and restore it afterwards. But if the contained code errors out, the umask is not restored. So add TRY/CATCH blocks to fix that.
1 parent 58ffe14 commit aa6b7b7

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/backend/commands/copy.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1826,7 +1826,16 @@ BeginCopyTo(ParseState *pstate,
18261826
errmsg("relative path not allowed for COPY to file")));
18271827

18281828
oumask = umask(S_IWGRP | S_IWOTH);
1829-
cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1829+
PG_TRY();
1830+
{
1831+
cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1832+
}
1833+
PG_CATCH();
1834+
{
1835+
umask(oumask);
1836+
PG_RE_THROW();
1837+
}
1838+
PG_END_TRY();
18301839
umask(oumask);
18311840
if (cstate->copy_file == NULL)
18321841
{

src/backend/libpq/be-fsstubs.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,17 @@ be_lo_export(PG_FUNCTION_ARGS)
538538
*/
539539
text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
540540
oumask = umask(S_IWGRP | S_IWOTH);
541-
fd = OpenTransientFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
542-
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
541+
PG_TRY();
542+
{
543+
fd = OpenTransientFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
544+
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
545+
}
546+
PG_CATCH();
547+
{
548+
umask(oumask);
549+
PG_RE_THROW();
550+
}
551+
PG_END_TRY();
543552
umask(oumask);
544553
if (fd < 0)
545554
ereport(ERROR,

0 commit comments

Comments
 (0)