mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4
@ 2013-09-27  8:54 Michael Forney
  2013-09-27 15:28 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Michael Forney @ 2013-09-27  8:54 UTC (permalink / raw)
  To: musl

If wn becomes 0 after processing a chunk of 4, mbsrtowcs currently
continues on, wrapping wn around to -1, causing the rest of the string
to be processed.

This resulted in buffer overruns if there was only space in ws for wn
wide characters.
---
Hi,

I found this bug while tracking down a SIGSEGV in bash when globbing a large
pattern.

 src/multibyte/mbsrtowcs.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
index b9bbc33..c5a30de 100644
--- a/src/multibyte/mbsrtowcs.c
+++ b/src/multibyte/mbsrtowcs.c
@@ -66,6 +66,7 @@ resume0:
 				*ws++ = *s++;
 				wn -= 4;
 			}
+			if (!wn) continue;
 		}
 		if (*s-1u < 0x7f) {
 			*ws++ = *s++;
-- 
1.8.4



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

* Re: [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4
  2013-09-27  8:54 [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4 Michael Forney
@ 2013-09-27 15:28 ` Rich Felker
  2013-09-27 15:56   ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2013-09-27 15:28 UTC (permalink / raw)
  To: musl

On Fri, Sep 27, 2013 at 01:54:42AM -0700, Michael Forney wrote:
> If wn becomes 0 after processing a chunk of 4, mbsrtowcs currently
> continues on, wrapping wn around to -1, causing the rest of the string
> to be processed.
> 
> This resulted in buffer overruns if there was only space in ws for wn
> wide characters.
> ---
> Hi,
> 
> I found this bug while tracking down a SIGSEGV in bash when globbing a large
> pattern.

Thanks! That's a nice find.

>  src/multibyte/mbsrtowcs.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
> index b9bbc33..c5a30de 100644
> --- a/src/multibyte/mbsrtowcs.c
> +++ b/src/multibyte/mbsrtowcs.c
> @@ -66,6 +66,7 @@ resume0:
>  				*ws++ = *s++;
>  				wn -= 4;
>  			}
> +			if (!wn) continue;

Rather than adding an extra branch here, why not just either change
the >=4 condition to >=5 or unconditionally continue here? Any
thoughts on what would be better?

Rich


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

* Re: [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4
  2013-09-27 15:28 ` Rich Felker
@ 2013-09-27 15:56   ` Rich Felker
  2013-09-27 22:43     ` Michael Forney
  0 siblings, 1 reply; 4+ messages in thread
From: Rich Felker @ 2013-09-27 15:56 UTC (permalink / raw)
  To: musl

On Fri, Sep 27, 2013 at 11:28:49AM -0400, Rich Felker wrote:
> >  src/multibyte/mbsrtowcs.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
> > index b9bbc33..c5a30de 100644
> > --- a/src/multibyte/mbsrtowcs.c
> > +++ b/src/multibyte/mbsrtowcs.c
> > @@ -66,6 +66,7 @@ resume0:
> >  				*ws++ = *s++;
> >  				wn -= 4;
> >  			}
> > +			if (!wn) continue;
> 
> Rather than adding an extra branch here, why not just either change
> the >=4 condition to >=5 or unconditionally continue here? Any
> thoughts on what would be better?

Forget what I said about just continuing; it would lead to an infinite
loop. I think checking for wn>=5 is probably the best solution to
avoid extra branches in a fairly common code path (ASCII at an aligned
position but not 4 ASCII characters in a row). At some point perhaps
all of this code should be reworked (with proper benchmarking to
measure the effect of changes) but for now we just need a fix.

Rich


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

* Re: [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4
  2013-09-27 15:56   ` Rich Felker
@ 2013-09-27 22:43     ` Michael Forney
  0 siblings, 0 replies; 4+ messages in thread
From: Michael Forney @ 2013-09-27 22:43 UTC (permalink / raw)
  To: musl

On Fri, 27 Sep 2013 11:56:18 -0400, Rich Felker <dalias@aerifal.cx> wrote:
> > Rather than adding an extra branch here, why not just either change
> > the >=4 condition to >=5 or unconditionally continue here? Any
> > thoughts on what would be better?
> 
> Forget what I said about just continuing; it would lead to an infinite
> loop. I think checking for wn>=5 is probably the best solution to
> avoid extra branches in a fairly common code path (ASCII at an aligned
> position but not 4 ASCII characters in a row). At some point perhaps
> all of this code should be reworked (with proper benchmarking to
> measure the effect of changes) but for now we just need a fix.
> 
> Rich

Yeah, I didn't make any performance considerations, I just figured this
would be the easiest/simplest fix. The solution you've committed is
fine. Thanks!

-- 
Michael Forney <mforney@mforney.org>


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

end of thread, other threads:[~2013-09-27 22:43 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-09-27  8:54 [PATCH] mbsrtowcs: Fix bug when wn is a multiple of 4 Michael Forney
2013-09-27 15:28 ` Rich Felker
2013-09-27 15:56   ` Rich Felker
2013-09-27 22:43     ` Michael Forney

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