Skip to content

Commit 9bf0499

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 dbd6099 commit 9bf0499

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
@@ -1619,7 +1619,16 @@ BeginCopyTo(Relation rel,
16191619
errmsg("relative path not allowed for COPY to file")));
16201620

16211621
oumask = umask(S_IWGRP | S_IWOTH);
1622-
cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1622+
PG_TRY();
1623+
{
1624+
cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
1625+
}
1626+
PG_CATCH();
1627+
{
1628+
umask(oumask);
1629+
PG_RE_THROW();
1630+
}
1631+
PG_END_TRY();
16231632
umask(oumask);
16241633
if (cstate->copy_file == NULL)
16251634
ereport(ERROR,

src/backend/libpq/be-fsstubs.c

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

0 commit comments

Comments
 (0)