mailing list of musl libc
 help / color / mirror / code / Atom feed
* [musl] [PATCH] getmntent_r: support empty source field
@ 2022-04-23 18:46 Oliver Ford
  0 siblings, 0 replies; only message in thread
From: Oliver Ford @ 2022-04-23 18:46 UTC (permalink / raw)
  To: musl

[-- 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 --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-04-23 18:46 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-23 18:46 [musl] [PATCH] getmntent_r: support empty source field Oliver Ford

Code repositories for project(s) associated with this 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).