Fix saving and restoring umask
authorPeter Eisentraut <[email protected]>
Fri, 22 Sep 2017 20:50:59 +0000 (16:50 -0400)
committerPeter Eisentraut <[email protected]>
Sat, 23 Sep 2017 14:14:30 +0000 (10:14 -0400)
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.

src/backend/commands/copy.c
src/backend/libpq/be-fsstubs.c

index 55192741aa6e8ad77c0858b303771c69cc723065..4a3d027ca3a5f796d46622d6b021d25fd4715149 100644 (file)
@@ -1423,7 +1423,16 @@ BeginCopyTo(Relation rel,
 
        cstate->filename = pstrdup(filename);
        oumask = umask(S_IWGRP | S_IWOTH);
-       cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
+       PG_TRY();
+       {
+           cstate->copy_file = AllocateFile(cstate->filename, PG_BINARY_W);
+       }
+       PG_CATCH();
+       {
+           umask(oumask);
+           PG_RE_THROW();
+       }
+       PG_END_TRY();
        umask(oumask);
 
        if (cstate->copy_file == NULL)
index 6f7e474f675485766c7ffca7312d0f1e0adbb83d..695a05d360c7d4b2b4d01a10e54912cde6f5f8eb 100644 (file)
@@ -474,8 +474,17 @@ lo_export(PG_FUNCTION_ARGS)
     */
    text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
    oumask = umask(S_IWGRP | S_IWOTH);
-   fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
-                         S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+   PG_TRY();
+   {
+       fd = PathNameOpenFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
+                             S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+   }
+   PG_CATCH();
+   {
+       umask(oumask);
+       PG_RE_THROW();
+   }
+   PG_END_TRY();
    umask(oumask);
    if (fd < 0)
        ereport(ERROR,