mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Oliver Ford <ojford@gmail.com>
To: musl@lists.openwall.com
Subject: [musl] [PATCH] getmntent_r: support empty source field
Date: Sat, 23 Apr 2022 19:46:32 +0100	[thread overview]
Message-ID: <CAGMVOduqaPzE4fGxx5_Reb1MUGaEKAkXD2beHzVZm0CwiYRFiw@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2103 bytes --]

Some mount types such as tmpfs allow the source field
to be an empty string. Handle the case of an empty source
field with a second sscanf if the first does not match every field.

Issue originally raised by Keyu Tao on 6th April. As per discussion,
octal escape characters are not yet supported but can be implemented
next.
---
 src/misc/mntent.c | 40 +++++++++++++++++++++++++++++-----------
 1 file changed, 29 insertions(+), 11 deletions(-)

diff --git a/src/misc/mntent.c b/src/misc/mntent.c
index eabb8200..43041881 100644
--- a/src/misc/mntent.c
+++ b/src/misc/mntent.c
@@ -21,7 +21,7 @@ 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 cnt, ch, n[8], use_internal = (linebuf == SENTINEL);

  mnt->mnt_freq = 0;
  mnt->mnt_passno = 0;
@@ -39,20 +39,38 @@ struct mntent *getmntent_r(FILE *f, struct mntent *mnt,
char *linebuf, int bufle
  errno = ERANGE;
  return 0;
  }
+
+ ch = strspn(linebuf, " ");
+ if (linebuf[ch] == '#') continue;
+
  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]] == '#');
-
- linebuf[n[1]] = 0;
- linebuf[n[3]] = 0;
- linebuf[n[5]] = 0;
- linebuf[n[7]] = 0;
+ if (cnt == 2) {
+ linebuf[n[1]] = 0;
+ linebuf[n[3]] = 0;
+ linebuf[n[5]] = 0;
+ linebuf[n[7]] = 0;
+ mnt->mnt_fsname = linebuf+n[0];
+ mnt->mnt_dir = linebuf+n[2];
+ mnt->mnt_type = linebuf+n[4];
+ mnt->mnt_opts = linebuf+n[6];
+ } else {
+ cnt = sscanf(linebuf, " %n%*s%n %n%*s%n %n%*s%n %d %d",
+ n, n+1, n+2, n+3, n+4, n+5, n+6,
+ &mnt->mnt_freq, &mnt->mnt_passno);

- mnt->mnt_fsname = linebuf+n[0];
- mnt->mnt_dir = linebuf+n[2];
- mnt->mnt_type = linebuf+n[4];
- mnt->mnt_opts = linebuf+n[6];
+ if (cnt == 2) {
+ linebuf[n[1]] = 0;
+ linebuf[n[3]] = 0;
+ linebuf[n[5]] = 0;
+ mnt->mnt_fsname = "";
+ mnt->mnt_dir = linebuf+n[0];
+ mnt->mnt_type = linebuf+n[2];
+ mnt->mnt_opts = linebuf+n[4];
+ }
+ }
+ } while (cnt < 2);

  return mnt;
 }
-- 
2.35.1

[-- Attachment #2: Type: text/html, Size: 5268 bytes --]

                 reply	other threads:[~2022-04-23 18:46 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=CAGMVOduqaPzE4fGxx5_Reb1MUGaEKAkXD2beHzVZm0CwiYRFiw@mail.gmail.com \
    --to=ojford@gmail.com \
    --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).