Skip to content

Commit 9c5327b

Browse files
committed
Pay attention to failure returns from fgets() in all cases.
Avoid infinite loop prompting for password at stdin EOF.
1 parent e8a72c0 commit 9c5327b

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/bin/psql/command.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.40 2000/11/26 11:09:32 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.41 2000/11/27 02:20:36 tgl Exp $
77
*/
88
#include "postgres.h"
99
#include "command.h"
@@ -1236,7 +1236,8 @@ do_connect(const char *new_dbname, const char *new_user)
12361236
NULL, NULL, dbparam, userparam, pwparam);
12371237

12381238
if (PQstatus(pset.db) == CONNECTION_BAD &&
1239-
strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
1239+
strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
1240+
!feof(stdin))
12401241
{
12411242
PQfinish(pset.db);
12421243
need_pass = true;
@@ -1491,7 +1492,7 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf)
14911492
char line[1024];
14921493

14931494
resetPQExpBuffer(query_buf);
1494-
while (fgets(line, 1024, stream))
1495+
while (fgets(line, sizeof(line), stream) != NULL)
14951496
appendPQExpBufferStr(query_buf, line);
14961497

14971498
if (ferror(stream))

src/bin/psql/common.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.26 2000/11/27 01:28:40 tgl Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.27 2000/11/27 02:20:36 tgl Exp $
77
*/
88
#include "postgres.h"
99
#include "common.h"
@@ -201,7 +201,8 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
201201
}
202202
#endif
203203

204-
fgets(destination, maxlen, stdin);
204+
if (fgets(destination, maxlen, stdin) == NULL)
205+
destination[0] = '\0';
205206

206207
#ifdef HAVE_TERMIOS_H
207208
if (!echo)
@@ -222,7 +223,8 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
222223

223224
do
224225
{
225-
fgets(buf, sizeof(buf), stdin);
226+
if (fgets(buf, sizeof(buf), stdin) == NULL)
227+
break;
226228
buflen = strlen(buf);
227229
} while (buflen > 0 && buf[buflen - 1] != '\n');
228230
}
@@ -389,9 +391,9 @@ SendQuery(const char *query)
389391
"***(press return to proceed or enter x and return to cancel)********************\n",
390392
query);
391393
fflush(stdout);
392-
fgets(buf, 3, stdin);
393-
if (buf[0] == 'x')
394-
return false;
394+
if (fgets(buf, sizeof(buf), stdin) != NULL)
395+
if (buf[0] == 'x')
396+
return false;
395397
}
396398
else
397399
{

src/bin/psql/input.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.13 2000/04/12 17:16:22 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/input.c,v 1.14 2000/11/27 02:20:36 tgl Exp $
77
*/
88
#include "postgres.h"
99
#include "input.h"
@@ -91,7 +91,7 @@ gets_fromFile(FILE *source)
9191

9292
initPQExpBuffer(&buffer);
9393

94-
while (fgets(line, 1024, source) != NULL)
94+
while (fgets(line, sizeof(line), source) != NULL)
9595
{
9696
appendPQExpBufferStr(&buffer, line);
9797
if (buffer.data[buffer.len - 1] == '\n')

src/bin/psql/prompt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.15 2000/11/13 23:37:53 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/prompt.c,v 1.16 2000/11/27 02:20:36 tgl Exp $
77
*/
88
#include "postgres.h"
99
#include "prompt.h"
@@ -277,7 +277,7 @@ get_prompt(promptStatus_t status)
277277
fgets(buf, MAX_PROMPT_SIZE - 1, fd);
278278
pclose(fd);
279279
}
280-
if (buf[strlen(buf) - 1] == '\n')
280+
if (strlen(buf) > 0 && buf[strlen(buf) - 1] == '\n')
281281
buf[strlen(buf) - 1] = '\0';
282282
free(file);
283283
p += cmdend + 1;

src/bin/psql/startup.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.40 2000/11/25 19:05:44 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/startup.c,v 1.41 2000/11/27 02:20:36 tgl Exp $
77
*/
88
#include "postgres.h"
99

@@ -180,7 +180,8 @@ main(int argc, char *argv[])
180180
username, password);
181181

182182
if (PQstatus(pset.db) == CONNECTION_BAD &&
183-
strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0)
183+
strcmp(PQerrorMessage(pset.db), "fe_sendauth: no password supplied\n") == 0 &&
184+
!feof(stdin))
184185
{
185186
PQfinish(pset.db);
186187
need_pass = true;

0 commit comments

Comments
 (0)