Fix saving and restoring umask
authorPeter Eisentraut <peter_e@gmx.net>
Fri, 22 Sep 2017 20:50:59 +0000 (16:50 -0400)
committerPeter Eisentraut <peter_e@gmx.net>
Sat, 23 Sep 2017 14:04:55 +0000 (10:04 -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 f93d4ed7cfc632d9e41b57efc60a5ee8235ee6f6..808ea09eeb2447bd6ca16d148e9244a78955e27a 100644 (file)
@@ -1764,7 +1764,16 @@ BeginCopyTo(Relation rel,
                      errmsg("relative path not allowed for COPY to file")));
 
            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)
                ereport(ERROR,
index 75e3b8da4be6cc73287796f3ed1a05d12427b329..c386ea8ad62d3c70229e3356cbcdbb02c52c7108 100644 (file)
@@ -540,8 +540,17 @@ lo_export(PG_FUNCTION_ARGS)
     */
    text_to_cstring_buffer(filename, fnamebuf, sizeof(fnamebuf));
    oumask = umask(S_IWGRP | S_IWOTH);
-   fd = OpenTransientFile(fnamebuf, O_CREAT | O_WRONLY | O_TRUNC | PG_BINARY,
-                          S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+   PG_TRY();
+   {
+       fd = OpenTransientFile(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,