Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/tre/dist/lib Make REG_STARTEND for tre_regcomp(...



details:   https://anonhg.NetBSD.org/src/rev/8018722834d0
branches:  trunk
changeset: 827777:8018722834d0
user:      rin <rin%NetBSD.org@localhost>
date:      Sun Nov 12 16:10:59 2017 +0000

description:
Make REG_STARTEND for tre_regcomp() working actually:
- correct string length
- shift pmatch after pattern matching
- check validity of arguments
Do the same thing for tre_regwcomp()

diffstat:

 external/bsd/tre/dist/lib/regexec.c |  65 +++++++++++++++++++++++++++++-------
 1 files changed, 52 insertions(+), 13 deletions(-)

diffs (86 lines):

diff -r 035657b3b8a0 -r 8018722834d0 external/bsd/tre/dist/lib/regexec.c
--- a/external/bsd/tre/dist/lib/regexec.c       Sun Nov 12 15:33:03 2017 +0000
+++ b/external/bsd/tre/dist/lib/regexec.c       Sun Nov 12 16:10:59 2017 +0000
@@ -198,22 +198,34 @@
 tre_regexec(const regex_t *preg, const char *str,
        size_t nmatch, regmatch_t pmatch[], int eflags)
 {
-       const char      *newstr;
-       unsigned         newflags;
-       size_t           newlen;
+       size_t shift, len, i;
+       int startend, ret;
+       tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
+       regmatch_t *p;
 
        if (eflags & REG_STARTEND) {
-               /* LINTED */
-               newstr = &str[pmatch[0].rm_so];
-               /* LINTED */
-               newlen = pmatch[0].rm_eo;
-               newflags = (unsigned)(eflags & ~REG_STARTEND);
+               if (pmatch == NULL || pmatch->rm_so < 0
+                   || pmatch->rm_so > pmatch->rm_eo)
+                       return REG_INVARG;
+               str += shift = pmatch->rm_so;
+               len = pmatch->rm_eo - pmatch->rm_so;
+               eflags = (unsigned)(eflags & ~REG_STARTEND);
+               startend = 1;
        } else {
-               newstr = str;
-               newlen = (size_t)-1;
-               newflags = (unsigned)eflags;
+               len = (size_t)-1;
+               startend = 0;
        }
-       return tre_regnexec(preg, newstr, newlen, nmatch, pmatch, (int)newflags);
+
+       ret = tre_regnexec(preg, str, len, nmatch, pmatch, eflags);
+
+       if (!ret && !(tnfa->cflags & REG_NOSUB) && startend) {
+               for (i = nmatch, p = pmatch; i > 0; p++, i--) {
+                       if (p->rm_so >= 0) p->rm_so += shift;
+                       if (p->rm_eo >= 0) p->rm_eo += shift;
+               }
+       }
+
+       return ret;
 }
 
 
@@ -231,7 +243,34 @@
 tre_regwexec(const regex_t *preg, const wchar_t *str,
         size_t nmatch, regmatch_t pmatch[], int eflags)
 {
-  return tre_regwnexec(preg, str, (unsigned)-1, nmatch, pmatch, eflags);
+       size_t shift, len, i;
+       int startend, ret;
+       tre_tnfa_t *tnfa = (void *)preg->TRE_REGEX_T_FIELD;
+       regmatch_t *p;
+
+       if (eflags & REG_STARTEND) {
+               if (pmatch == NULL || pmatch->rm_so < 0
+                   || pmatch->rm_so > pmatch->rm_eo)
+                       return REG_INVARG;
+               str += shift = pmatch->rm_so;
+               len = pmatch->rm_eo - pmatch->rm_so;
+               eflags = (unsigned)(eflags & ~REG_STARTEND);
+               startend = 1;
+       } else {
+               len = (size_t)-1;
+               startend = 0;
+       }
+
+       ret = tre_regwnexec(preg, str, len, nmatch, pmatch, eflags);
+
+       if (!ret && !(tnfa->cflags & REG_NOSUB) && startend) {
+               for (i = nmatch, p = pmatch; i > 0; p++, i--) {
+                       if (p->rm_so >= 0) p->rm_so += shift;
+                       if (p->rm_eo >= 0) p->rm_eo += shift;
+               }
+       }
+
+       return ret;
 }
 
 #endif /* TRE_WCHAR */



Home | Main Index | Thread Index | Old Index