Commit c5bbbb28 authored by Tom Lane's avatar Tom Lane

Pay attention to fgets() failure return.

parent a568b227
...@@ -44,6 +44,7 @@ _prompt_for_password(char *username, char *password) ...@@ -44,6 +44,7 @@ _prompt_for_password(char *username, char *password)
{ {
char buf[512]; char buf[512];
int length; int length;
int buflen;
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
struct termios t_orig, struct termios t_orig,
...@@ -57,15 +58,18 @@ _prompt_for_password(char *username, char *password) ...@@ -57,15 +58,18 @@ _prompt_for_password(char *username, char *password)
{ {
fprintf(stderr, "Username: "); fprintf(stderr, "Username: ");
fflush(stderr); fflush(stderr);
fgets(username, 100, stdin); if (fgets(username, 100, stdin) == NULL)
username[0] = '\0';
length = strlen(username); length = strlen(username);
/* skip rest of the line */
if (length > 0 && username[length - 1] != '\n') if (length > 0 && username[length - 1] != '\n')
{ {
/* eat rest of the line */
do do
{ {
fgets(buf, 512, stdin); if (fgets(buf, sizeof(buf), stdin) == NULL)
} while (buf[strlen(buf) - 1] != '\n'); break;
buflen = strlen(buf);
} while (buflen > 0 && buf[buflen - 1] != '\n');
} }
if (length > 0 && username[length - 1] == '\n') if (length > 0 && username[length - 1] == '\n')
username[length - 1] = '\0'; username[length - 1] = '\0';
...@@ -79,19 +83,22 @@ _prompt_for_password(char *username, char *password) ...@@ -79,19 +83,22 @@ _prompt_for_password(char *username, char *password)
#endif #endif
fprintf(stderr, "Password: "); fprintf(stderr, "Password: ");
fflush(stderr); fflush(stderr);
fgets(password, 100, stdin); if (fgets(password, 100, stdin) == NULL)
password[0] = '\0';
#ifdef HAVE_TERMIOS_H #ifdef HAVE_TERMIOS_H
tcsetattr(0, TCSADRAIN, &t_orig); tcsetattr(0, TCSADRAIN, &t_orig);
#endif #endif
length = strlen(password); length = strlen(password);
/* skip rest of the line */
if (length > 0 && password[length - 1] != '\n') if (length > 0 && password[length - 1] != '\n')
{ {
/* eat rest of the line */
do do
{ {
fgets(buf, 512, stdin); if (fgets(buf, sizeof(buf), stdin) == NULL)
} while (buf[strlen(buf) - 1] != '\n'); break;
buflen = strlen(buf);
} while (buflen > 0 && buf[buflen - 1] != '\n');
} }
if (length > 0 && password[length - 1] == '\n') if (length > 0 && password[length - 1] == '\n')
password[length - 1] = '\0'; password[length - 1] = '\0';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment