diff mbox series

[uclibc-ng-devel] hasmntopt: better option matching

Message ID 20201019060954.1377364-1-yann@sionneau.net
State Accepted
Headers show
Series [uclibc-ng-devel] hasmntopt: better option matching | expand

Commit Message

Yann Sionneau Oct. 19, 2020, 6:09 a.m. UTC
From: Yann Sionneau <yann@sionneau.net>

Previous implementation was respecting the man page description
of what the function should do.
Also the function does not seem to be defined by POSIX.

But... to be really useful the function needs to handle
option matching and not just substring matching.

This is copy pasted from glibc.

This fixes issue reported by https://github.com/wbx-github/uclibc-ng/issues/8
that can happen for instance there: https://github.com/frida/glib/blob/master/gio/gunixmounts.c#L622

Signed-off-by: Yann Sionneau <yann@sionneau.net>
---
 libc/misc/mntent/mntent.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

Comments

Waldemar Brodkorb Oct. 19, 2020, 8:07 a.m. UTC | #1
Hi Yann,

yann@sionneau.net wrote,

> From: Yann Sionneau <yann@sionneau.net>
> 
> Previous implementation was respecting the man page description
> of what the function should do.
> Also the function does not seem to be defined by POSIX.
> 
> But... to be really useful the function needs to handle
> option matching and not just substring matching.
> 
> This is copy pasted from glibc.
> 
> This fixes issue reported by https://github.com/wbx-github/uclibc-ng/issues/8
> that can happen for instance there: https://github.com/frida/glib/blob/master/gio/gunixmounts.c#L622
> 
> Signed-off-by: Yann Sionneau <yann@sionneau.net>

Applied and pushed,
 thx 
  Waldemar
diff mbox series

Patch

diff --git a/libc/misc/mntent/mntent.c b/libc/misc/mntent/mntent.c
index 608f84c1a..610e35af6 100644
--- a/libc/misc/mntent/mntent.c
+++ b/libc/misc/mntent/mntent.c
@@ -1,5 +1,6 @@ 
 /*
  * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
+ * Copyright (C) 1995-2020 Free Software Foundation, Inc.
  *
  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  */
@@ -92,9 +93,25 @@  int addmntent(FILE * filep, const struct mntent *mnt)
 		 mnt->mnt_type, mnt->mnt_opts, mnt->mnt_freq, mnt->mnt_passno) < 0 ? 1 : 0);
 }
 
-char *hasmntopt(const struct mntent *mnt, const char *opt)
+/* Search MNT->mnt_opts for an option matching OPT.
+   Returns the address of the substring, or null if none found.  */
+char *hasmntopt (const struct mntent *mnt, const char *opt)
 {
-	return strstr(mnt->mnt_opts, opt);
+    const size_t optlen = strlen(opt);
+    char *rest = mnt->mnt_opts, *p;
+
+    while ((p = strstr(rest, opt)) != NULL) {
+        if ((p == rest || p[-1] == ',')
+            && (p[optlen] == '\0' || p[optlen] == '=' || p[optlen] == ','))
+            return p;
+
+        rest = strchr(p, ',');
+        if (rest == NULL)
+            break;
+        ++rest;
+    }
+
+    return NULL;
 }
 
 FILE *setmntent(const char *name, const char *mode)