mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: "Érico Nogueira" <ericonr@disroot.org>
Cc: musl@lists.openwall.com, Alyssa Ross <hi@alyssa.is>
Subject: Re: [musl] [PATCH musl 3/3] mntent: fix parsing lines with optional fields
Date: Fri, 27 Aug 2021 12:49:28 -0400	[thread overview]
Message-ID: <20210827164928.GW13220@brightrain.aerifal.cx> (raw)
In-Reply-To: <CDUEG1A8S5HX.7ER2VIN313Q3@mussels>

On Fri, Aug 27, 2021 at 12:27:36PM -0300, Érico Nogueira wrote:
> On Sat Aug 21, 2021 at 5:54 AM -03, 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.
> 
> Besides typing change noted below, the change sounds reasonable.
> 
> If you want to fix another issue around mntent, hasmntopts has some
> troubles too :p
> 
> > ---
> >
> > 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.
> >
> > 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..5a68f0b9 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 use_internal = (linebuf == SENTINEL);
> > + size_t len, i, n[8];
> 
> Try avoiding unrelated changes in the commit, since they can introduce
> subtle bugs. In this case, making n size_t[] instead of int[] will lead
> to pointer type mismatches in the sscanf call, given that %n expects an
> int*.
> 
> I don't know if *scanf guarantees it won't read enough to go past

For *scanf in general there is no such guarantee; not even size_t is
safe for fscanf. However, here you have sscanf and the number is
bounded by strlen(linebuf).

> INT_MAX, though, so making a change to size_t[] and using %ln might make
> sense. Deferring to someone else to answer that.

The conversion specifier for size_t is %zu not %ln. Since in theory
strlen(linebuf) could be more than INT_MAX, I think this change should
be made, but it should be a separate bugfix.

Rich

  reply	other threads:[~2021-08-27 16:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-21  8:54 [musl] [PATCH 0/3] " Alyssa Ross
2021-08-21  8:54 ` [musl] [PATCH libc-test 1/3] functional: add mntent test Alyssa Ross
2021-08-21  8:54 ` [musl] [PATCH libc-test 2/3] functional: add mntent test for single-field line Alyssa Ross
2021-08-21  8:54 ` [musl] [PATCH musl 3/3] mntent: fix parsing lines with optional fields Alyssa Ross
2021-08-27 15:27   ` Érico Nogueira
2021-08-27 16:49     ` Rich Felker [this message]
2021-08-27 16:59       ` Rich Felker

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=20210827164928.GW13220@brightrain.aerifal.cx \
    --to=dalias@libc.org \
    --cc=ericonr@disroot.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).