mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] glob: fix / matching
@ 2017-05-19 14:59 Julien Ramseier
  2017-05-28  1:59 ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Julien Ramseier @ 2017-05-19 14:59 UTC (permalink / raw)
  To: musl

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

glob(3) currently fails matching "/", due to any '/' being
removed from the start of the pattern before checking if
it's empty.


[-- Attachment #2: glob.patch --]
[-- Type: application/octet-stream, Size: 1416 bytes --]

diff --git a/src/regex/glob.c b/src/regex/glob.c
index 5b6ff12..53f3a4e 100644
--- a/src/regex/glob.c
+++ b/src/regex/glob.c
@@ -156,18 +156,11 @@ static int sort(const void *a, const void *b)
 
 int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
 {
-	const char *p=pat, *d;
+	const char *p = pat;
 	struct match head = { .next = NULL }, *tail = &head;
 	size_t cnt, i;
 	size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
 	int error = 0;
-	
-	if (*p == '/') {
-		for (; *p == '/'; p++);
-		d = "/";
-	} else {
-		d = "";
-	}
 
 	if (!errfunc) errfunc = ignore_err;
 
@@ -179,12 +172,19 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i
 
 	if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
 
-	if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
+	if (*p) {
+		const char *d = "";
+		if (*p == '/') {
+			for (; *p == '/'; p++);
+			d = "/";
+		}
+		error = match_in_dir(d, p, flags, errfunc, &tail);
+	}
 	if (error == GLOB_NOSPACE) {
 		freelist(&head);
 		return error;
 	}
-	
+
 	for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
 	if (!cnt) {
 		if (flags & GLOB_NOCHECK) {
@@ -220,7 +220,7 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i
 
 	if (!(flags & GLOB_NOSORT))
 		qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
-	
+
 	return error;
 }
 

[-- Attachment #3: Type: text/plain, Size: 100 bytes --]



Also, any feedback concerning
http://www.openwall.com/lists/musl/2017/01/12/5 ? 

Thanks,
Julien


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

* Re: [PATCH] glob: fix / matching
  2017-05-19 14:59 [PATCH] glob: fix / matching Julien Ramseier
@ 2017-05-28  1:59 ` Rich Felker
  2017-05-28  2:55   ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Rich Felker @ 2017-05-28  1:59 UTC (permalink / raw)
  To: musl

On Fri, May 19, 2017 at 04:59:13PM +0200, Julien Ramseier wrote:
> glob(3) currently fails matching "/", due to any '/' being
> removed from the start of the pattern before checking if
> it's empty.
> 

> diff --git a/src/regex/glob.c b/src/regex/glob.c
> index 5b6ff12..53f3a4e 100644
> --- a/src/regex/glob.c
> +++ b/src/regex/glob.c
> @@ -156,18 +156,11 @@ static int sort(const void *a, const void *b)
>  
>  int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
>  {
> -	const char *p=pat, *d;
> +	const char *p = pat;
>  	struct match head = { .next = NULL }, *tail = &head;
>  	size_t cnt, i;
>  	size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
>  	int error = 0;
> -	
> -	if (*p == '/') {
> -		for (; *p == '/'; p++);
> -		d = "/";
> -	} else {
> -		d = "";
> -	}
>  
>  	if (!errfunc) errfunc = ignore_err;
>  
> @@ -179,12 +172,19 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i
>  
>  	if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
>  
> -	if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
> +	if (*p) {
> +		const char *d = "";
> +		if (*p == '/') {
> +			for (; *p == '/'; p++);
> +			d = "/";
> +		}
> +		error = match_in_dir(d, p, flags, errfunc, &tail);
> +	}

I'm confused how this patch differs from just removing the "if (*p)"
condition before calling match_in_dir. Does match_in_dir actually work
if p points to an empty string? I thought not...

> Also, any feedback concerning
> http://www.openwall.com/lists/musl/2017/01/12/5 ? 

I'll have to actually trace through what it does to figure out why
that's happening; the intent was that it work for all components, I
think..

Really the whole glob() implementation could use a complete rewrite.

Rich


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

* Re: [PATCH] glob: fix / matching
  2017-05-28  1:59 ` Rich Felker
@ 2017-05-28  2:55   ` Rich Felker
  2017-05-28 15:06     ` Julien Ramseier
  0 siblings, 1 reply; 6+ messages in thread
From: Rich Felker @ 2017-05-28  2:55 UTC (permalink / raw)
  To: musl

On Sat, May 27, 2017 at 09:59:18PM -0400, Rich Felker wrote:
> On Fri, May 19, 2017 at 04:59:13PM +0200, Julien Ramseier wrote:
> > glob(3) currently fails matching "/", due to any '/' being
> > removed from the start of the pattern before checking if
> > it's empty.
> > 
> 
> > diff --git a/src/regex/glob.c b/src/regex/glob.c
> > index 5b6ff12..53f3a4e 100644
> > --- a/src/regex/glob.c
> > +++ b/src/regex/glob.c
> > @@ -156,18 +156,11 @@ static int sort(const void *a, const void *b)
> >  
> >  int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
> >  {
> > -	const char *p=pat, *d;
> > +	const char *p = pat;
> >  	struct match head = { .next = NULL }, *tail = &head;
> >  	size_t cnt, i;
> >  	size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
> >  	int error = 0;
> > -	
> > -	if (*p == '/') {
> > -		for (; *p == '/'; p++);
> > -		d = "/";
> > -	} else {
> > -		d = "";
> > -	}
> >  
> >  	if (!errfunc) errfunc = ignore_err;
> >  
> > @@ -179,12 +172,19 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i
> >  
> >  	if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
> >  
> > -	if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
> > +	if (*p) {
> > +		const char *d = "";
> > +		if (*p == '/') {
> > +			for (; *p == '/'; p++);
> > +			d = "/";
> > +		}
> > +		error = match_in_dir(d, p, flags, errfunc, &tail);
> > +	}
> 
> I'm confused how this patch differs from just removing the "if (*p)"
> condition before calling match_in_dir. Does match_in_dir actually work
> if p points to an empty string? I thought not...

Hmm, just removing the "if (*p)" seems to make it work; it looks like
match_in_dir covers this case fine. I'd like a second opinion on
whether this looks okay since I haven't touched this code in years,
but I suspect it'll turn out to be an okay fix for now...

> > Also, any feedback concerning
> > http://www.openwall.com/lists/musl/2017/01/12/5 ? 
> 
> I'll have to actually trace through what it does to figure out why
> that's happening; the intent was that it work for all components, I
> think..
> 
> Really the whole glob() implementation could use a complete rewrite.

...but I still think the code is due for a rewrite. Just the fact that
it collapses all ///// to / is rather bad.

Rich


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

* Re: [PATCH] glob: fix / matching
  2017-05-28  2:55   ` Rich Felker
@ 2017-05-28 15:06     ` Julien Ramseier
  2017-06-01  2:00       ` Rich Felker
  0 siblings, 1 reply; 6+ messages in thread
From: Julien Ramseier @ 2017-05-28 15:06 UTC (permalink / raw)
  To: musl

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


> Le 28 mai 2017 à 04:55, Rich Felker <dalias@libc.org> a écrit :
>> 
>> I'm confused how this patch differs from just removing the "if (*p)"
>> condition before calling match_in_dir. Does match_in_dir actually work
>> if p points to an empty string? I thought not...
> 
> Hmm, just removing the "if (*p)" seems to make it work; it looks like
> match_in_dir covers this case fine. I'd like a second opinion on
> whether this looks okay since I haven't touched this code in years,
> but I suspect it'll turn out to be an okay fix for now...
> 


The "if (*p)" is needed, otherwise match_in_dir will append an empty
match when p = "". 

- Julien

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

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

* Re: [PATCH] glob: fix / matching
  2017-05-28 15:06     ` Julien Ramseier
@ 2017-06-01  2:00       ` Rich Felker
  2017-06-03 16:15         ` Julien Ramseier
  0 siblings, 1 reply; 6+ messages in thread
From: Rich Felker @ 2017-06-01  2:00 UTC (permalink / raw)
  To: musl

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

On Sun, May 28, 2017 at 05:06:45PM +0200, Julien Ramseier wrote:
> 
> > Le 28 mai 2017 à 04:55, Rich Felker <dalias@libc.org> a écrit :
> >> 
> >> I'm confused how this patch differs from just removing the "if (*p)"
> >> condition before calling match_in_dir. Does match_in_dir actually work
> >> if p points to an empty string? I thought not...
> > 
> > Hmm, just removing the "if (*p)" seems to make it work; it looks like
> > match_in_dir covers this case fine. I'd like a second opinion on
> > whether this looks okay since I haven't touched this code in years,
> > but I suspect it'll turn out to be an okay fix for now...
> > 
> 
> 
> The "if (*p)" is needed, otherwise match_in_dir will append an empty
> match when p = "". 

Indeed. How about the attached?

I did look at your patch, and although it's probably fine, it does
have additional side effects like moving the length check across the
slash-skipping. That's part of why I looked for a minimally invasive
approach.

Rich

[-- Attachment #2: globroot.diff --]
[-- Type: text/plain, Size: 484 bytes --]

diff --git a/src/regex/glob.c b/src/regex/glob.c
index 5b6ff12..2d4d562 100644
--- a/src/regex/glob.c
+++ b/src/regex/glob.c
@@ -179,7 +179,7 @@ int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, i
 
 	if (strnlen(p, PATH_MAX+1) > PATH_MAX) return GLOB_NOSPACE;
 
-	if (*p) error = match_in_dir(d, p, flags, errfunc, &tail);
+	if (*pat) error = match_in_dir(d, p, flags, errfunc, &tail);
 	if (error == GLOB_NOSPACE) {
 		freelist(&head);
 		return error;

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

* Re: [PATCH] glob: fix / matching
  2017-06-01  2:00       ` Rich Felker
@ 2017-06-03 16:15         ` Julien Ramseier
  0 siblings, 0 replies; 6+ messages in thread
From: Julien Ramseier @ 2017-06-03 16:15 UTC (permalink / raw)
  To: musl

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


> Le 1 juin 2017 à 04:00, Rich Felker <dalias@libc.org> a écrit :
> 
> Indeed. How about the attached?
> 
> I did look at your patch, and although it's probably fine, it does
> have additional side effects like moving the length check across the
> slash-skipping. That's part of why I looked for a minimally invasive
> approach.
> 
> Rich
> <globroot.diff>

Yes, this should work too.


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

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

end of thread, other threads:[~2017-06-03 16:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-19 14:59 [PATCH] glob: fix / matching Julien Ramseier
2017-05-28  1:59 ` Rich Felker
2017-05-28  2:55   ` Rich Felker
2017-05-28 15:06     ` Julien Ramseier
2017-06-01  2:00       ` Rich Felker
2017-06-03 16:15         ` Julien Ramseier

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).