mntent: fix parsing lines with optional fields
[musl] / src / misc / mntent.c
index eabb820..962b876 100644 (file)
@@ -21,7 +21,8 @@ int endmntent(FILE *f)
 
 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
 {
-       int cnt, n[8], use_internal = (linebuf == SENTINEL);
+       int n[8], use_internal = (linebuf == SENTINEL);
+       size_t len, i;
 
        mnt->mnt_freq = 0;
        mnt->mnt_passno = 0;
@@ -39,10 +40,13 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle
                        errno = ERANGE;
                        return 0;
                }
-               cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
+
+               len = strlen(linebuf);
+               for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
+               sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
                        n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
                        &mnt->mnt_freq, &mnt->mnt_passno);
-       } while (cnt < 2 || linebuf[n[0]] == '#');
+       } while (linebuf[n[0]] == '#' || n[1]==len);
 
        linebuf[n[1]] = 0;
        linebuf[n[3]] = 0;