Skip to content

Commit c5bbbb2

Browse files
committed
Pay attention to fgets() failure return.
1 parent a568b22 commit c5bbbb2

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/bin/pg_dump/pg_backup_db.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ _prompt_for_password(char *username, char *password)
4444
{
4545
char buf[512];
4646
int length;
47+
int buflen;
4748

4849
#ifdef HAVE_TERMIOS_H
4950
struct termios t_orig,
@@ -57,15 +58,18 @@ _prompt_for_password(char *username, char *password)
5758
{
5859
fprintf(stderr, "Username: ");
5960
fflush(stderr);
60-
fgets(username, 100, stdin);
61+
if (fgets(username, 100, stdin) == NULL)
62+
username[0] = '\0';
6163
length = strlen(username);
62-
/* skip rest of the line */
6364
if (length > 0 && username[length - 1] != '\n')
6465
{
66+
/* eat rest of the line */
6567
do
6668
{
67-
fgets(buf, 512, stdin);
68-
} while (buf[strlen(buf) - 1] != '\n');
69+
if (fgets(buf, sizeof(buf), stdin) == NULL)
70+
break;
71+
buflen = strlen(buf);
72+
} while (buflen > 0 && buf[buflen - 1] != '\n');
6973
}
7074
if (length > 0 && username[length - 1] == '\n')
7175
username[length - 1] = '\0';
@@ -79,19 +83,22 @@ _prompt_for_password(char *username, char *password)
7983
#endif
8084
fprintf(stderr, "Password: ");
8185
fflush(stderr);
82-
fgets(password, 100, stdin);
86+
if (fgets(password, 100, stdin) == NULL)
87+
password[0] = '\0';
8388
#ifdef HAVE_TERMIOS_H
8489
tcsetattr(0, TCSADRAIN, &t_orig);
8590
#endif
8691

8792
length = strlen(password);
88-
/* skip rest of the line */
8993
if (length > 0 && password[length - 1] != '\n')
9094
{
95+
/* eat rest of the line */
9196
do
9297
{
93-
fgets(buf, 512, stdin);
94-
} while (buf[strlen(buf) - 1] != '\n');
98+
if (fgets(buf, sizeof(buf), stdin) == NULL)
99+
break;
100+
buflen = strlen(buf);
101+
} while (buflen > 0 && buf[buflen - 1] != '\n');
95102
}
96103
if (length > 0 && password[length - 1] == '\n')
97104
password[length - 1] = '\0';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy