Commit Graph

36 Commits

Author SHA1 Message Date
Yuri Pankov
3c78771400 lib/libc/regex: fix build with REDEBUG defined
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D21760
2019-09-24 12:21:01 +00:00
Yuri Pankov
63cbe8d1d9 regexec: fix processing multibyte strings.
Matcher function incorrectly assumed that moffset that we get from
findmust is in bytes. Fix this by introducing a stepback function,
taking short path if MB_CUR_MAX is 1, and going back byte-by-byte,
checking if we have a legal character sequence otherwise.

PR:		153502
Reviewed by:	pfg, kevans
Approved by:	kib (mentor, implicit)
Differential revision:	https://reviews.freebsd.org/D18297
2018-11-23 15:49:18 +00:00
Toomas Soome
307546ec52 regex/engine.c: error: variable 'dp' set but not used
The issue found with gcc6 build (originally on illumos, confirmed on FreeBSD).
Mark it __unused.

Differential Revision:	https://reviews.freebsd.org/D13109
2018-07-14 09:29:45 +00:00
Kyle Evans
4f8f1c798e regex(3): Resolve issues with higher WARNS levels
libc is set for WARNS=2, but the incoming libregex will use WARNS=6.
Sprinkle some casts and (void)bc's to alleviate the warnings that come along
with the higher WARNS level.

These 'bc' parameters could be outright removed, but as of right now they
will be used in some parts of libregex land. Silence the warnings instead
rather than flip-flopping.
2018-01-21 04:57:29 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Kyle Evans
a0bf5d8a68 regex(3): Refactor fast/slow stepping bits in the matching engine
Adding features for matching is fairly straightforward, but this requires
some duplication because of this fast/slow setup. They can be fairly
trivially combined into a single walk(), so do it to make future additions
less error prone.

Reviewed by:	cem (earlier version), emaste, pfg
Approved by:	emaste (mentor)
Differential Revision:	https://reviews.freebsd.org/D11233
2017-08-09 01:04:36 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Pedro F. Giffuni
93ea9f9fa1 libc: regexec(3) adjustment.
Change the behavior of when REG_STARTEND is combined with REG_NOTBOL.

From the original posting[1]:

"Enable the assumption that pmatch[0].rm_so is a continuation offset
to  a string and allows us to do a proper assessment of the character
in  regards to it's word position ('^' or '\<'), without risking going
into unallocated memory."

This change makes us similar to how glibc handles REG_STARTEND |
REG_NOTBOL, and is closely related to a soon-to-land fix to sed.

Special thanks to Martijn van Duren and Ingo Schwarze for working
out some consistent behaviour.

Differential Revision:	https://reviews.freebsd.org/D6257
Taken from:	openbsd-tech 2016-05-24 [1]  (Martijn van Duren)
Relnotes:	yes
MFC after:	1 month
2016-05-25 15:35:23 +00:00
Pedro F. Giffuni
e9fe9edde7 libc/regex: fix two buffer underruns.
Fix some rather complex regex issues found on OpenBSD as part of some
ongoing work to fix a sed(1) bug.

Curiously the OpenBSD tests don't trigger segfaults on FreeBSD but the
bugs were confirmed by running a port of FreeBSD's regex under OpenBSD's
malloc. Huge thanks to Ingo for confirming the behavior.

Taken from:	Ingo Schwarze (through openbsd-tech 2016-05-15)
MFC after:	1 week
2016-05-21 19:54:10 +00:00
Pedro F. Giffuni
039b877ef0 regex: prevent two improbable signed integer overflows.
In matcher() we used an integer to index nsub of type size_t.
In print() we used an integer to index nstates of type sopno,
typedef'd long.
In both cases the indexes never take negative values.

Match the types to avoid any error.

MFC after:	5 days
2016-04-23 20:45:09 +00:00
Pedro F. Giffuni
cc3a001f1c Prevent NULL pointer de-reference.
As a follow up to r279090, if dp hasn't been defined, we
shouldn't attempt to do an optimization here.
2015-02-21 15:02:27 +00:00
Pedro F. Giffuni
d58abbfe0f regex(3): Fix uninitialized pointer values.
CID:	405582	(also clang static checker)
CID:	1018724
2015-02-20 21:21:38 +00:00
Pedro F. Giffuni
eab20bceca regex: Remove some unreachable breaks.
This is based on a much bigger cleanup done in Illumos.

Reference:
https://www.illumos.org/issues/2077

MFC after:	1 week
2014-05-01 23:34:14 +00:00
Diomidis Spinellis
e7cbc6ee95 Fix an off-by-one error in the marking of the O_CH operator
following an OOR2 operator.

PR:		130504
MFC after:	2 weeks
2009-09-16 06:32:23 +00:00
Diomidis Spinellis
bca3476acd Add a couple of debugging statements. 2009-09-16 06:29:23 +00:00
Xin LI
eb2b3d109a Const'ify and ANSIfy the internal interfaces of regex(3).
This is the final change that makes libc to compile with
WERROR on my amd64 crashbox.
2007-05-25 12:44:58 +00:00
Xin LI
082063a051 Only stop evaluation of a back reference if the match length is
zero and the recursion level is too deep.

Obtained from:	OpenBSD
2007-03-05 09:43:55 +00:00
Xin LI
0f4481c5e4 Avoid infinite recursion on:
echo "foo foo bar bar bar baz" | sed 's/\([^ ]*\)\( *\1\)*/\1/g'

Obtained from:	OpenBSD via NetBSD (rev. 1.18)
2007-03-05 03:07:36 +00:00
Warner Losh
c879ae3536 Per Regents of the University of Calfornia letter, remove advertising
clause.

# If I've done so improperly on a file, please let me know.
2007-01-09 00:28:16 +00:00
Tim J. Robbins
1ee0dbee57 Fix a boundary condition error in slow() and fast() in multibyte locales:
we must allow the character beginning at "p" to be converted to a wide
character for the purposes of EOL processing and word-boundary matching.
2005-08-17 11:37:38 +00:00
Diomidis Spinellis
c7ce9e21f8 Plug memory leak.
PR:		bin/75656
MFC after:	2 weeks
2004-12-30 14:27:57 +00:00
Tim J. Robbins
bd9643b135 Fix computation of the 'n' argument to mbrtowc (through XMBRTOWC) to avoid
reading past 'stop' in various places when converting multibyte characters.
Reading too far caused truncation to not be detected when it should have
been, eventually causing regexec() to loop infinitely in with certain
combinations of patterns and strings in multibyte locales.

PR:		74020
MFC after:	4 weeks
2004-11-21 03:14:32 +00:00
Tim J. Robbins
e5996857ad Make regular expression matching aware of multibyte characters. The general
idea is that we perform multibyte->wide character conversion while parsing
and compiling, then convert byte sequences to wide characters when they're
needed for comparison and stepping through the string during execution.

As with tr(1), the main complication is to efficiently represent sets of
characters in bracket expressions. The old bitmap representation is replaced
by a bitmap for the first 256 characters combined with a vector of individual
wide characters, a vector of character ranges (for [A-Z] etc.), and a vector
of character classes (for [[:alpha:]] etc.).

One other point of interest is that although the Boyer-Moore algorithm had
to be disabled in the general multibyte case, it is still enabled for UTF-8
because of its self-synchronizing nature. This greatly speeds up matching
by reducing the number of multibyte conversions that need to be done.
2004-07-12 07:35:59 +00:00
Jacques Vidrine
e0554a531f Eliminate 61 warnings emitted at WARNS=2 (leaving 53 to go).
Only warnings that could be fixed without changing the generated object
code and without restructuring the source code have been handled.

Reviewed by:	/sbin/md5
2003-02-16 17:29:11 +00:00
David E. O'Brien
333fc21e3c Fix the style of the SCM ID's.
I believe have made all of libc .c's as consistent as possible.
2002-03-22 21:53:29 +00:00
David E. O'Brien
c05ac53b8b Remove __P() usage. 2002-03-21 22:49:10 +00:00
David E. O'Brien
8fb3f3f682 Remove 'register' keyword. 2002-03-21 18:49:23 +00:00
Daniel C. Sobral
b6c1a56180 Since g->moffset points to the _maximum_ offset at which the must
string may be found (from the beginning of the pattern), the point
at which must is found minus that offset may actually point to some
place before the start of the text.

In that case, make start = start.

Alternatively, this could be tested for in the preceding if, but it
did not occur to me. :-)

Caught by: regex(3) test code
2000-07-09 17:36:53 +00:00
Daniel C. Sobral
c5e125bbbf Deal with the signed/unsigned chars issue in a more proper manner. We
use a CHAR_MIN-based array, like elsewhere in the code.

Remove a number of unused variables (some due to the above change, one
that was left after a number of optimizing steps through the source).

Brucified by: bde
2000-07-07 07:46:36 +00:00
Daniel C. Sobral
e6a886d8db Enhance the optimization provided by pre-matching. Fix style bugs with
previous commits.

At the time we search the pattern for the "must" string, we now compute
the longest offset from the beginning of the pattern at which the must
string might be found. If that offset is found to be infinite (through
use of "+" or "*"), we set it to -1 to disable the heuristics applied
later.

After we are done with pre-matching, we use that offset and the point in
the text at which the must string was found to compute the earliest
point at which the pattern might be found.

Special care should be taken here. The variable "start" is passed to the
automata-processing functions fast() and slow() to indicate the point in
the text at which they should start working from. The real beginning of
the text is passed in a struct match variable m, which is used to check
for anchors. That variable, though, is initialized with "start", so we
must not adjust "start" before "m" is properly initialized.

Simple tests showed a speed increase from 100% to 400%, but they were
biased in that regexec() was called for the whole file instead of line
by line, and parenthized subexpressions were not searched for.

This change adds a single integer to the size of the "guts" structure,
and does not change the ABI.

Further improvements possible:

Since the speed increase observed here is so huge, one intuitive
optimization would be to introduce a bias in the function that computes
the "must" string so as to prefer a smaller string with a finite offset
over a larger one with an infinite offset. Tests have shown this to be a
bad idea, though, as the cost of false pre-matches far outweights the
benefits of a must offset, even in biased situations.

A number of other improvements suggest themselves, though:

	* identify the cases where the pattern is identical to the must
	string, and avoid entering fast() and slow() in these cases.

	* compute the maximum offset from the must string to the end of
	the pattern, and use that to set the point at which fast() and
	slow() should give up trying to find a match, and return then
	return to pre-matching.

	* return all the way to pre-matching if a "match" was found and
	later invalidated by back reference processing. Since back
	references are evil and should be avoided anyway, this is of
	little use.
2000-07-02 10:58:07 +00:00
Daniel C. Sobral
6049d9f0eb Add Boyler-Moore algorithm to pre-matching test.
The BM algorithm works by scanning the pattern from right to left,
and jumping as many characters as viable based on the text's mismatched
character and the pattern's already matched suffix.

This typically enable us to test only a fraction of the text's characters,
but has a worse performance than the straight-forward method for small
patterns. Because of this, the BM algorithm will only be used if the
pattern size is at least 4 characters.

Notice that this pre-matching is done on the largest substring of the
regular expression that _must_ be present on the text for a succesful
match to be possible at all.

For instance, "(xyzzy|grues)" will yield a null "must" substring, and,
therefore, not benefit from the BM algorithm at all. Because of the
lack of intelligence of the algorithm that finds the "must" string,
things like "charjump|matchjump" will also yield a null string. To
optimize that, "(char|match)jump" should be used.

The setup time (at regcomp()) for the BM algorithm will most likely
outweight any benefits for one-time matches. Given the slow regex(3)
we have, this is unlikely to be even perceptible, though.

The size of a regex_t structure is increased by 2*sizeof(char*) +
256*sizeof(int) + strlen(must)*sizeof(int). This is all inside the
regex_t's "guts", which is allocated dynamically by regcomp(). If
allocation of either of the two tables fail, the other one is freed.
In this case, the straight-forward algorithm is used for pre-matching.

Tests exercising the code path affected have shown a speed increase of
50% for "must" strings of length four or five.

API and ABI remain unchanged by this commit.

The patch submitted on the PR was not used, as it was non-functional.

PR: 14342
2000-06-29 04:48:34 +00:00
Andrey A. Chernov
b5363c4a3b Use locale for character classes instead of hardcoded values
Misc 8bit cleanup
1996-08-11 11:42:03 +00:00
Jordan K. Hubbard
51295a4d3e General -Wall warning cleanup, part I.
Submitted-By: Kent Vander Velden <graphix@iastate.edu>
1996-07-12 18:57:58 +00:00
Poul-Henning Kamp
16252f1166 More cleanup.
Uhm, I also forgot: I took "EXTRA_SANITY" out of malloc.c
1995-10-22 14:40:55 +00:00
Rodney W. Grimes
6c06b4e2aa Remove trailing whitespace. 1995-05-30 05:51:47 +00:00
Rodney W. Grimes
58f0484fa2 BSD 4.4 Lite Lib Sources 1994-05-27 05:00:24 +00:00