From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Cc: Alyssa Ross <hi@alyssa.is>
Subject: Re: [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields
Date: Sat, 8 Jan 2022 22:18:19 -0500 [thread overview]
Message-ID: <20220109031819.GO7074@brightrain.aerifal.cx> (raw)
In-Reply-To: <20210920042140.GT13220@brightrain.aerifal.cx>
On Mon, Sep 20, 2021 at 12:21:41AM -0400, Rich Felker wrote:
> On Wed, Sep 15, 2021 at 10:11:55PM +0000, Alyssa Ross wrote:
> > According to fstab(5), the last two fields are optional, but this
> > wasn't accepted by Musl. After this change, only the first field is
> > required, which matches Glibc's behaviour.
> >
> > Using sscanf as before, it would have been impossible to differentiate
> > between 0 fields and 4 fields, because sscanf would have returned 0 in
> > both cases due to the use of assignment suppression and %n for the
> > string fields (which is important to avoid copying any strings). So
> > instead, before calling sscanf, initialize every string to the empty
> > string, and then we can check which strings are empty afterwards to
> > know how many fields were matched.
> > ---
> >
> > We could also be stricter about it, and enforce that the first four
> > fields are present, since the man page says only the last two are
> > optional. Doing that would be a simple change of checking for the
> > presence of mnt_opts instead of mnt_fsname at the end of my patch.
> >
> > v2: don't change n from int to size_t
> >
> > src/misc/mntent.c | 18 +++++++++++++-----
> > 1 file changed, 13 insertions(+), 5 deletions(-)
> >
> > diff --git a/src/misc/mntent.c b/src/misc/mntent.c
> > index eabb8200..238a0efd 100644
> > --- a/src/misc/mntent.c
> > +++ b/src/misc/mntent.c
> > @@ -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,14 @@ 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",
> > - 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]] == '#');
> > +
> > + len = strlen(linebuf);
> > + for (i = 0; i < sizeof n / sizeof *n; i++) n[i] = len;
> > + if (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) == EOF && ferror(f))
> > + return 0;
> > + } while (linebuf[n[0]] == '#');
> >
> > linebuf[n[1]] = 0;
> > linebuf[n[3]] = 0;
> > @@ -54,6 +60,9 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt, char *linebuf, int bufle
> > mnt->mnt_type = linebuf+n[4];
> > mnt->mnt_opts = linebuf+n[6];
> >
> > + if (!*mnt->mnt_fsname)
> > + return 0;
> > +
> > return mnt;
> > }
>
> It looks like your patch changes the behavior for malformed lines from
> skipping them (and continuing to search for the next valid line) to
> returning 0. Is that intentional? Maybe it's better; I'm not sure. But
> won't it even cause blank lines to return 0?
Indeed it also seems to be skipping empty lines, contrary to what you
said in another message:
> • Empty lines should be skipped.
Do you have a preference on how to proceed? We could add back a
condition to the while loop, something like linebuf[n[0]]=='#' ||
n[6]==len (i.e. skip lines with too few fields, possibly using a
different number instead of 6 if more appropriate). Or we could do
what I suggested before:
> A less invasive change might be adding "%1[ \t\n\v\f\r]" and a dummy
> char* argument to collect the value before the " %d %d". Then you can
> check for cnt<1. But I'm not sure even the 4th field should be
> mandatory. This same apprach could be used to make just 3 mandatory if
> desired though.
Thoughts?
Rich
next prev parent reply other threads:[~2022-01-09 3:18 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-15 22:11 [musl] [PATCH v2 0/3] Alyssa Ross
2021-09-15 22:11 ` [musl] [PATCH libc-test v2 1/3] functional: add mntent test Alyssa Ross
2021-09-15 22:11 ` [musl] [PATCH libc-test v2 2/3] functional: add mntent test for single-field line Alyssa Ross
2021-09-15 22:11 ` [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields Alyssa Ross
2021-09-20 4:21 ` Rich Felker
2022-01-09 3:18 ` Rich Felker [this message]
2022-01-13 16:30 ` Alyssa Ross
2022-01-13 17:40 ` Rich Felker
2022-01-13 18:53 ` Alyssa Ross
2022-05-12 14:08 ` Rich Felker
2022-05-12 18:02 ` Alyssa Ross
2022-05-12 18:15 ` Rich Felker
2022-05-15 18:38 ` Alyssa Ross
2022-05-15 23:19 ` Rich Felker
2022-05-16 10:19 ` Alyssa Ross
2022-05-12 20:58 ` Oliver Ford
2022-05-12 21:10 ` Rich Felker
2022-05-13 21:39 ` Oliver Ford
2022-05-14 4:24 ` Rich Felker
2022-05-14 22:16 ` Oliver Ford
2022-05-15 23:31 ` Rich Felker
2022-05-16 10:07 ` Alyssa Ross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220109031819.GO7074@brightrain.aerifal.cx \
--to=dalias@libc.org \
--cc=hi@alyssa.is \
--cc=musl@lists.openwall.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/musl/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).