Whamcloud - gitweb
Check fgets(3) return value
authorDmitry V. Levin <ldv@altlinux.org>
Sat, 20 Oct 2007 18:08:40 +0000 (22:08 +0400)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 29 Oct 2007 14:59:01 +0000 (10:59 -0400)
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>
debugfs/debugfs.c
lib/ext2fs/ismounted.c
misc/util.c

index 190c4b7..d701615 100644 (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])
index 24246e0..cb563fd 100644 (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)
index 6a4c40c..7c99a2a 100644 (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);
 }