From 43b7595fdcc69cc9db0d066a53f53c5e71c965aa Mon Sep 17 00:00:00 2001 From: Marti Raudsepp Date: Wed, 20 Oct 2010 23:44:36 +0300 Subject: [PATCH] psql: Don't close stdin, don't leak file descriptor with ON_ERROR_STOP Changes three things: 1. Don't close stdin when returning success if input was stdin 2. Don't leak file descriptor when exiting due to ON_ERROR_STOP 3. pset.inputfile wasn't being restored with ON_ERROR_STOP, yet the memory was freed by the caller (1) changes the behavior of "\q" on stdin. 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. (2) and (3) should have no practical impact, because the process would quit immediately afterwards anyway. --- src/bin/psql/command.c | 15 ++++++++++++--- 1 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index e6d703a..45a145c 100644 --- a/src/bin/psql/command.c +++ b/src/bin/psql/command.c @@ -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; } -- 1.7.3.1