From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/10580 Path: news.gmane.org!.POSTED!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: [PATCH] regex: REG_STARTEND support Date: Wed, 5 Oct 2016 12:23:05 -0400 Message-ID: <20161005162305.GI19318@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: blaine.gmane.org 1475684610 4548 195.159.176.226 (5 Oct 2016 16:23:30 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 5 Oct 2016 16:23:30 +0000 (UTC) User-Agent: Mutt/1.5.21 (2010-09-15) Cc: musl@lists.openwall.com, Johannes.Schindelin@gmx.de To: Julien Ramseier Original-X-From: musl-return-10592-gllmg-musl=m.gmane.org@lists.openwall.com Wed Oct 05 18:23:26 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by blaine.gmane.org with smtp (Exim 4.84_2) (envelope-from ) id 1broyT-00084y-WB for gllmg-musl@m.gmane.org; Wed, 05 Oct 2016 18:23:18 +0200 Original-Received: (qmail 3478 invoked by uid 550); 5 Oct 2016 16:23:18 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 3457 invoked from network); 5 Oct 2016 16:23:17 -0000 Content-Disposition: inline In-Reply-To: Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:10580 Archived-At: On Wed, Oct 05, 2016 at 02:19:35PM +0200, Julien Ramseier wrote: > Here's my REG_STARTEND patch, mostly copied from the original tre[1] > implementation. > It's only lightly tested. > [...] > diff --git a/src/regex/regexec.c b/src/regex/regexec.c > index 16c5d0a..ae65726 100644 > --- a/src/regex/regexec.c > +++ b/src/regex/regexec.c > @@ -29,6 +29,7 @@ > > */ > > +#include > #include > #include > #include > @@ -51,11 +52,15 @@ tre_fill_pmatch(size_t nmatch, regmatch_t pmatch[], int cflags, > > #define GET_NEXT_WCHAR() do { \ > prev_c = next_c; pos += pos_add_next; \ > - if ((pos_add_next = mbtowc(&next_c, str_byte, MB_LEN_MAX)) <= 0) { \ > - if (pos_add_next < 0) { ret = REG_NOMATCH; goto error_exit; } \ > - else pos_add_next++; \ > + if (len >= 0 && pos >= len) \ > + next_c = L'\0'; \ As caught discussing this on #musl yesterday, pos (int) here has the wrong type, int, which is a big problem. I'm going to work on a test case to show it and confirm that changing the type fixes it. > + else { \ > + if ((pos_add_next = mbtowc(&next_c, str_byte, MB_LEN_MAX)) <= 0) { \ > + if (pos_add_next < 0) { ret = REG_NOMATCH; goto error_exit; } \ > + else pos_add_next++; \ > + } \ > + str_byte += pos_add_next; \ > } \ > - str_byte += pos_add_next; \ There also seems to be a bug, which was also present in the original TRE I think, whereby read past len can happen if the buffer up to len ends with a partial multibyte character. Avoiding this seems rather costly. Otherwise this doesn't look too bad. I'll see if we can get some figures for how it affects performance. Rich