mailing list of musl libc
 help / color / mirror / Atom feed
* [musl] [PATCH v2 0/3]
@ 2021-09-15 22:11 Alyssa Ross
  2021-09-15 22:11 ` [musl] [PATCH libc-test v2 1/3] functional: add mntent test Alyssa Ross
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Alyssa Ross @ 2021-09-15 22:11 UTC (permalink / raw)
  To: musl; +Cc: Érico Nogueira, Rich Felker

v2: don't change n from int to size_t
v1: https://inbox.vuxu.org/musl/20210821085420.474615-1-hi@alyssa.is/

This series introduces tests for libc-test that demonstrate a Musl
bug, and follows them with a fix for that bug.

-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [musl] [PATCH libc-test v2 1/3] functional: add mntent test
  2021-09-15 22:11 [musl] [PATCH v2 0/3] Alyssa Ross
@ 2021-09-15 22:11 ` 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
  2 siblings, 0 replies; 5+ messages in thread
From: Alyssa Ross @ 2021-09-15 22:11 UTC (permalink / raw)
  To: musl; +Cc: Érico Nogueira, Rich Felker

This only checks reading an fstab from an stream.  I haven't written
tests for either setmntent(), addmntent(), or hasmntnent().

test_getmntent exposes a bug in Musl where lines omitting the final
two fields, which are supposed to be optional according to fstab(5),
are not accepted.  The tests all pass on Glibc.
---
 AUTHORS                 |  1 +
 src/functional/mntent.c | 76 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+)
 create mode 100644 src/functional/mntent.c

diff --git a/AUTHORS b/AUTHORS
index ff99471..cf2a394 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -5,3 +5,4 @@ John Spencer
 Jens Gustedt
 Alexander Monakov
 Julien Ramseier
+Alyssa Ross
diff --git a/src/functional/mntent.c b/src/functional/mntent.c
new file mode 100644
index 0000000..59d816a
--- /dev/null
+++ b/src/functional/mntent.c
@@ -0,0 +1,76 @@
+// SPDX-License-Identifier: MIT
+
+#define _DEFAULT_SOURCE // for getmntent_r
+
+#include <mntent.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include "test.h"
+
+#define ASSERT(x) do {				 \
+		if (!(x)) {			 \
+			t_error(#x " failed\n"); \
+			exit(EXIT_FAILURE);	 \
+		}				 \
+	} while (0);
+
+#define ERR(fmt, ...) do {					       \
+		t_error(fmt ": %s\n", ##__VA_ARGS__, strerror(errno)); \
+		exit(EXIT_FAILURE);				       \
+	} while (0)
+
+void test_getmntent_empty(void)
+{
+	char fstab[] = "\n";
+	FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+	if (!f) ERR("fmemopen");
+	ASSERT(!getmntent(f));
+	ASSERT(endmntent(f) == 1);
+}
+
+void test_getmntent(void)
+{
+	// Checks that the fifth and sixth fields default to 0.
+	char fstab[] = "none /proc proc defaults\n";
+	FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+	if (!f) ERR("fmemopen");
+	struct mntent *m = getmntent(f);
+	ASSERT(m);
+	ASSERT(!strcmp(m->mnt_fsname, "none"));
+	ASSERT(!strcmp(m->mnt_dir, "/proc"));
+	ASSERT(!strcmp(m->mnt_type, "proc"));
+	ASSERT(!strcmp(m->mnt_opts, "defaults"));
+	ASSERT(m->mnt_freq == 0);
+	ASSERT(m->mnt_passno == 0);
+	ASSERT(endmntent(f) == 1);
+}
+
+void test_getmntent_r(void)
+{
+	struct mntent m, *r;
+	char fstab[] = "/dev/sda\t/\text4\trw,nosuid\t2\t1\n";
+	char buf[sizeof(fstab)];
+
+	FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+	if (!f) ERR("fmemopen");
+
+	r = getmntent_r(f, &m, buf, sizeof buf);
+	ASSERT(r == &m);
+	ASSERT(!strcmp(m.mnt_fsname, "/dev/sda"));
+	ASSERT(!strcmp(m.mnt_dir, "/"));
+	ASSERT(!strcmp(m.mnt_type, "ext4"));
+	ASSERT(!strcmp(m.mnt_opts, "rw,nosuid"));
+	ASSERT(m.mnt_freq == 2);
+	ASSERT(m.mnt_passno == 1);
+	ASSERT(endmntent(f) == 1);
+}
+
+int main(void)
+{
+	test_getmntent_empty();
+	test_getmntent();
+	test_getmntent_r();
+}
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [musl] [PATCH libc-test v2 2/3] functional: add mntent test for single-field line
  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 ` Alyssa Ross
  2021-09-15 22:11 ` [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields Alyssa Ross
  2 siblings, 0 replies; 5+ messages in thread
From: Alyssa Ross @ 2021-09-15 22:11 UTC (permalink / raw)
  To: musl; +Cc: Érico Nogueira, Rich Felker

Glibc only requires a single field to be present in an fstab line to
parse it, and will initialize all other string fields to the empty
string.  This test checks for that behaviour.
---

I'm providing this test as a seperate patch to make it easy to pass on
this test while still accepting the rest, because I'm not sure whether
it's a good thing or not for Musl to allow fstab lines like this, even
though Glibc does.

 src/functional/mntent.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/src/functional/mntent.c b/src/functional/mntent.c
index 59d816a..caa7d33 100644
--- a/src/functional/mntent.c
+++ b/src/functional/mntent.c
@@ -31,6 +31,18 @@ void test_getmntent_empty(void)
 	ASSERT(endmntent(f) == 1);
 }
 
+void test_getmntent_short(void)
+{
+	char fstab[] = "1\n";
+	FILE *f = fmemopen((void *)fstab, sizeof fstab - 1, "r");
+	if (!f) ERR("fmemopen");
+	struct mntent *m = getmntent(f);
+	ASSERT(m);
+	ASSERT(!strcmp(m->mnt_fsname, "1"));
+	ASSERT(!*m->mnt_dir);
+	ASSERT(endmntent(f) == 1);
+}
+
 void test_getmntent(void)
 {
 	// Checks that the fifth and sixth fields default to 0.
@@ -71,6 +83,7 @@ void test_getmntent_r(void)
 int main(void)
 {
 	test_getmntent_empty();
+	test_getmntent_short();
 	test_getmntent();
 	test_getmntent_r();
 }
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields
  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 ` Alyssa Ross
  2021-09-20  4:21   ` Rich Felker
  2 siblings, 1 reply; 5+ messages in thread
From: Alyssa Ross @ 2021-09-15 22:11 UTC (permalink / raw)
  To: musl; +Cc: Érico Nogueira, Rich Felker

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;
 }
 
-- 
2.32.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [musl] [PATCH musl v2 3/3] mntent: fix parsing lines with optional fields
  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
  0 siblings, 0 replies; 5+ messages in thread
From: Rich Felker @ 2021-09-20  4:21 UTC (permalink / raw)
  To: musl; +Cc: Alyssa Ross

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?

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.

Rich

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-09-20  4:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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

mailing list of musl libc

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/musl

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 musl musl/ https://inbox.vuxu.org/musl \
		musl@inbox.vuxu.org
	public-inbox-index musl

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.musl


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/musl/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git