Minor fixups for psql's process_file() function.
authorRobert Haas <[email protected]>
Tue, 26 Oct 2010 23:28:18 +0000 (19:28 -0400)
committerRobert Haas <[email protected]>
Tue, 26 Oct 2010 23:35:33 +0000 (19:35 -0400)
- Avoid closing stdin, since we didn't open it.  Previously multiple
inclusions of stdin would be terminated with a single quit, now a separate
quit is needed for each invocation. Previous behavior also accessed stdin
after it was fclose()d, which is undefined behavior per ANSI C.

- Properly restore pset.inputfile, since the caller expects to be able
to free that memory.

Marti Raudsepp

src/bin/psql/command.c

index e6d703abe740f2cb308a106b0f8013e252d2915c..fe37be66f2a854c8d1921327cee53754a568eb17 100644 (file)
@@ -1987,7 +1987,10 @@ process_file(char *filename, bool single_txn)
        if ((res = PSQLexec("BEGIN", false)) == NULL)
        {
            if (pset.on_error_stop)
-               return EXIT_USER;
+           {
+               result = EXIT_USER;
+               goto error;
+           }
        }
        else
            PQclear(res);
@@ -2000,13 +2003,19 @@ process_file(char *filename, bool single_txn)
        if ((res = PSQLexec("COMMIT", false)) == NULL)
        {
            if (pset.on_error_stop)
-               return EXIT_USER;
+           {
+               result = EXIT_USER;
+               goto error;
+           }
        }
        else
            PQclear(res);
    }
 
-   fclose(fd);
+error:
+   if (fd != stdin)
+       fclose(fd);
+
    pset.inputfile = oldfilename;
    return result;
 }