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

Commitc6f0bff

Browse files
committed
Pay attention to fgets() failure return.
1 parentc5bbbb2 commitc6f0bff

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

‎src/bin/pg_passwd/pg_passwd.c

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ read_pwd_file(char *filename)
6666
{
6767
printf("File \"%s\" does not exist. Create? (y/n): ",filename);
6868
fflush(stdout);
69-
fgets(ans,128,stdin);
69+
if (fgets(ans,sizeof(ans),stdin)==NULL)
70+
exit(1);
7071
switch (ans[0])
7172
{
7273
case'y':
@@ -260,18 +261,23 @@ prompt_for_username(char *username)
260261
intlength;
261262

262263
printf("Username: ");
263-
fgets(username,9,stdin);
264-
length=strlen(username);
264+
fflush(stdout);
265+
if (fgets(username,9,stdin)==NULL)
266+
username[0]='\0';
265267

266-
/* skip rest of the line */
268+
length=strlen(username);
267269
if (length>0&&username[length-1]!='\n')
268270
{
269-
staticcharbuf[512];
271+
/* eat rest of the line */
272+
charbuf[128];
273+
intbuflen;
270274

271275
do
272276
{
273-
fgets(buf,512,stdin);
274-
}while (buf[strlen(buf)-1]!='\n');
277+
if (fgets(buf,sizeof(buf),stdin)==NULL)
278+
break;
279+
buflen=strlen(buf);
280+
}while (buflen>0&&buf[buflen-1]!='\n');
275281
}
276282
if (length>0&&username[length-1]=='\n')
277283
username[length-1]='\0';
@@ -289,27 +295,32 @@ prompt_for_password(char *prompt, char *password)
289295
#endif
290296

291297
printf(prompt);
298+
fflush(stdout);
292299
#ifdefHAVE_TERMIOS_H
293300
tcgetattr(0,&t);
294301
t_orig=t;
295302
t.c_lflag &= ~ECHO;
296303
tcsetattr(0,TCSADRAIN,&t);
297304
#endif
298-
fgets(password,9,stdin);
305+
if (fgets(password,9,stdin)==NULL)
306+
password[0]='\0';
299307
#ifdefHAVE_TERMIOS_H
300308
tcsetattr(0,TCSADRAIN,&t_orig);
301309
#endif
302310

303311
length=strlen(password);
304-
/* skip rest of the line */
305312
if (length>0&&password[length-1]!='\n')
306313
{
307-
staticcharbuf[512];
314+
/* eat rest of the line */
315+
charbuf[128];
316+
intbuflen;
308317

309318
do
310319
{
311-
fgets(buf,512,stdin);
312-
}while (buf[strlen(buf)-1]!='\n');
320+
if (fgets(buf,sizeof(buf),stdin)==NULL)
321+
break;
322+
buflen=strlen(buf);
323+
}while (buflen>0&&buf[buflen-1]!='\n');
313324
}
314325
if (length>0&&password[length-1]=='\n')
315326
password[length-1]='\0';

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp