Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc5bbbb2

Browse files
committed
Pay attention to fgets() failure return.
1 parenta568b22 commitc5bbbb2

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
charbuf[512];
4646
intlength;
47+
intbuflen;
4748

4849
#ifdefHAVE_TERMIOS_H
4950
structtermiost_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
#ifdefHAVE_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)

[8]ページ先頭

©2009-2025 Movatter.jp