Check fgets(3) return value

When fgets() function fails, contents of the buffer is undefined.  That
is, fgets() return value needs to be checked, to avoid undefined behavior.

Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
bitmap-optimize
Dmitry V. Levin 2007-10-20 22:08:40 +04:00 committed by Theodore Ts'o
parent 84d51a2e99
commit d9039ae0ff
3 changed files with 10 additions and 9 deletions

View File

@ -789,7 +789,8 @@ static void modify_u8(char *com, const char *prompt,
sprintf(buf, format, *val);
printf("%30s [%s] ", prompt, buf);
fgets(buf, sizeof(buf), stdin);
if (!fgets(buf, sizeof(buf), stdin))
return;
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
if (!buf[0])
@ -810,7 +811,8 @@ static void modify_u16(char *com, const char *prompt,
sprintf(buf, format, *val);
printf("%30s [%s] ", prompt, buf);
fgets(buf, sizeof(buf), stdin);
if (!fgets(buf, sizeof(buf), stdin))
return;
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
if (!buf[0])
@ -831,7 +833,8 @@ static void modify_u32(char *com, const char *prompt,
sprintf(buf, format, *val);
printf("%30s [%s] ", prompt, buf);
fgets(buf, sizeof(buf), stdin);
if (!fgets(buf, sizeof(buf), stdin))
return;
if (buf[strlen (buf) - 1] == '\n')
buf[strlen (buf) - 1] = '\0';
if (!buf[0])

View File

@ -251,10 +251,8 @@ static int is_swap_device(const char *file)
if (!(f = fopen("/proc/swaps", "r")))
return 0;
/* Skip the first line */
fgets(buf, sizeof(buf), f);
while (!feof(f)) {
if (!fgets(buf, sizeof(buf), f))
break;
if (fgets(buf, sizeof(buf), f))
while (fgets(buf, sizeof(buf), f)) {
if ((cp = strchr(buf, ' ')) != NULL)
*cp = 0;
if ((cp = strchr(buf, '\t')) != NULL)

View File

@ -71,8 +71,8 @@ void proceed_question(void)
fflush(stderr);
fputs(_("Proceed anyway? (y,n) "), stdout);
buf[0] = 0;
fgets(buf, sizeof(buf), stdin);
if (strchr(short_yes, buf[0]) == 0)
if (!fgets(buf, sizeof(buf), stdin) ||
strchr(short_yes, buf[0]) == 0)
exit(1);
}