Pay attention to failure returns from fgets() in all cases.
authorTom Lane
Mon, 27 Nov 2000 02:20:36 +0000 (02:20 +0000)
committerTom Lane
Mon, 27 Nov 2000 02:20:36 +0000 (02:20 +0000)
Avoid infinite loop prompting for password at stdin EOF.

src/bin/psql/command.c
src/bin/psql/common.c
src/bin/psql/input.c
src/bin/psql/prompt.c
src/bin/psql/startup.c

index b17d37b674d0c29639213e433251ef456e09ef8d..782c161c23a2f95bc294e34907d9982ae46aa5f2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.40 2000/11/26 11:09:32 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.41 2000/11/27 02:20:36 tgl Exp $
  */
 #include "postgres.h"
 #include "command.h"
@@ -1236,7 +1236,8 @@ do_connect(const char *new_dbname, const char *new_user)
                               NULL, NULL, dbparam, userparam, pwparam);
 
        if (PQstatus(pset.db) == CONNECTION_BAD &&
-           strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
+           strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
+           !feof(stdin))
        {
            PQfinish(pset.db);
            need_pass = true;
@@ -1491,7 +1492,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
            char        line[1024];
 
            resetPQExpBuffer(query_buf);
-           while (fgets(line, 1024, stream))
+           while (fgets(line, sizeof(line), stream) != NULL)
                appendPQExpBufferStr(query_buf, line);
 
            if (ferror(stream))
index 66eeb0ef6b237a2702f3ef9a9bdd15310e50a181..05b11651e9de0b02d5514a1968ab67425ec28a96 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.26 2000/11/27 01:28:40 tgl Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.27 2000/11/27 02:20:36 tgl Exp $
  */
 #include "postgres.h"
 #include "common.h"
@@ -201,7 +201,8 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
    }
 #endif
 
-   fgets(destination, maxlen, stdin);
+   if (fgets(destination, maxlen, stdin) == NULL)
+       destination[0] = '\0';
 
 #ifdef HAVE_TERMIOS_H
    if (!echo)
@@ -222,7 +223,8 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
 
        do
        {
-           fgets(buf, sizeof(buf), stdin);
+           if (fgets(buf, sizeof(buf), stdin) == NULL)
+               break;
            buflen = strlen(buf);
        } while (buflen > 0 && buf[buflen - 1] != '\n');
    }
@@ -389,9 +391,9 @@ SendQuery(const char *query)
               "***(press return to proceed or enter x and return to cancel)********************\n",
               query);
        fflush(stdout);
-       fgets(buf, 3, stdin);
-       if (buf[0] == 'x')
-           return false;
+       if (fgets(buf, sizeof(buf), stdin) != NULL)
+           if (buf[0] == 'x')
+               return false;
    }
    else
    {
index 86200ad0522d9e0b3ff28af9ef5646f18a4bb2d4..78e2ae3568a198e120eb1940f3c5e79216c50815 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.13 2000/04/12 17:16:22 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.14 2000/11/27 02:20:36 tgl Exp $
  */
 #include "postgres.h"
 #include "input.h"
@@ -91,7 +91,7 @@ gets_fromFile(FILE *source)
 
    initPQExpBuffer(&buffer);
 
-   while (fgets(line, 1024, source) != NULL)
+   while (fgets(line, sizeof(line), source) != NULL)
    {
        appendPQExpBufferStr(&buffer, line);
        if (buffer.data[buffer.len - 1] == '\n')
index 4788c2ce128df94dc4e39dcd9fa52b77d9b0833d..7927a9caa29cc2114b57f3d4084ccb78d7208cac 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.15 2000/11/13 23:37:53 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.16 2000/11/27 02:20:36 tgl Exp $
  */
 #include "postgres.h"
 #include "prompt.h"
@@ -277,7 +277,7 @@ get_prompt(promptStatus_t status)
                            fgets(buf, MAX_PROMPT_SIZE - 1, fd);
                            pclose(fd);
                        }
-                       if (buf[strlen(buf) - 1] == '\n')
+                       if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
                            buf[strlen(buf) - 1] = '\0';
                        free(file);
                        p += cmdend + 1;
index 1a66d5fce361d854cfbaf95df640bcd2232a88bd..e551347371057645274b7efa74d76bbd8c674618 100644 (file)
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.40 2000/11/25 19:05:44 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.41 2000/11/27 02:20:36 tgl Exp $
  */
 #include "postgres.h"
 
@@ -180,7 +180,8 @@ main(int argc, char *argv[])
                               username, password);
 
        if (PQstatus(pset.db) == CONNECTION_BAD &&
-           strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
+           strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
+           !feof(stdin))
        {
            PQfinish(pset.db);
            need_pass = true;