mntent: fix potential mishandling of extremely long lines
[musl.git] / src / misc / mntent.c
blobd404fbe3929b0aa0b68c1a44046bfa5202069d68
1 #include <stdio.h>
2 #include <string.h>
3 #include <mntent.h>
4 #include <errno.h>
5 #include <limits.h>
7 static char *internal_buf;
8 static size_t internal_bufsize;
10 #define SENTINEL (char *)&internal_buf
12 FILE *setmntent(const char *name, const char *mode)
14 return fopen(name, mode);
17 int endmntent(FILE *f)
19 if (f) fclose(f);
20 return 1;
23 struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int buflen)
25 int n[8], use_internal = (linebuf == SENTINEL);
26 size_t len, i;
28 mnt->mnt_freq = 0;
29 mnt->mnt_passno = 0;
31 do {
32 if (use_internal) {
33 getline(&internal_buf, &internal_bufsize, f);
34 linebuf = internal_buf;
35 } else {
36 fgets(linebuf, buflen, f);
38 if (feof(f) || ferror(f)) return 0;
39 if (!strchr(linebuf, '\n')) {
40 fscanf(f, "%*[^\n]%*[\n]");
41 errno = ERANGE;
42 return 0;
45 len = strlen(linebuf);
46 if (len > INT_MAX) continue;
47 for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
48 sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %n%*s%n %d %d",
49 n, n+1, n+2, n+3, n+4, n+5, n+6, n+7,
50 &mnt->mnt_freq, &mnt->mnt_passno);
51 } while (linebuf[n[0]] == '#' || n[1]==len);
53 linebuf[n[1]] = 0;
54 linebuf[n[3]] = 0;
55 linebuf[n[5]] = 0;
56 linebuf[n[7]] = 0;
58 mnt->mnt_fsname = linebuf+n[0];
59 mnt->mnt_dir = linebuf+n[2];
60 mnt->mnt_type = linebuf+n[4];
61 mnt->mnt_opts = linebuf+n[6];
63 return mnt;
66 struct mntent *getmntent(FILE *f)
68 static struct mntent mnt;
69 return getmntent_r(f, &mnt, SENTINEL, 0);
72 int addmntent(FILE *f, const struct mntent *mnt)
74 if (fseek(f, 0, SEEK_END)) return 1;
75 return fprintf(f, "%s\t%s\t%s\t%s\t%d\t%d\n",
76 mnt->mnt_fsname, mnt->mnt_dir, mnt->mnt_type, mnt->mnt_opts,
77 mnt->mnt_freq, mnt->mnt_passno) < 0;
80 char *hasmntopt(const struct mntent *mnt, const char *opt)
82 return strstr(mnt->mnt_opts, opt);