OSSP CVS Repository

ossp - Check-in [1530]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 1530
Date: 2002-Jan-09 12:27:07 (local)
2002-Jan-09 11:27:07 (UTC)
User:rse
Branch:
Comment: - Compile regular expressions with REG_NEWLINE flag so that '^' and '$' match in the middle of the string where appropriate.

- Fixed incorrect results of search_and_replace() with zero-length matches, such as '^'.

Tickets:
Inspections:
Files:
ossp-pkg/var/var.c      1.58 -> 1.59     22 inserted, 6 deleted

ossp-pkg/var/var.c 1.58 -> 1.59

--- var.c        2002/01/02 17:12:18     1.58
+++ var.c        2002/01/09 11:27:07     1.59
@@ -1272,7 +1272,7 @@
 
         /* Compile the pattern. */
 
-        rc = regcomp(&preg, tmp.begin, REG_EXTENDED|((case_insensitive)?REG_ICASE:0));
+        rc = regcomp(&preg, tmp.begin, REG_NEWLINE | REG_EXTENDED|((case_insensitive)?REG_ICASE:0));
         tokenbuf_free(&tmp);
         if (rc != 0) {
             tokenbuf_free(&mydata);
@@ -1287,9 +1287,8 @@
                 regexec_flag = 0;
             else
                 regexec_flag = REG_NOTBOL;
-            if (regexec
-                (&preg, p, sizeof(pmatch) / sizeof(regmatch_t), pmatch,
-                 regexec_flag) == REG_NOMATCH) {
+            if (regexec(&preg, p, sizeof(pmatch) / sizeof(regmatch_t), pmatch, regexec_flag) == REG_NOMATCH ||
+                p + pmatch[0].rm_so == mydata.end) {
                 tokenbuf_append(&tmp, p, mydata.end - p);
                 break;
             } else {
@@ -1309,11 +1308,28 @@
                     tokenbuf_free(&myreplace);
                     return VAR_ERR_OUT_OF_MEMORY;
                 } else {
-                    p += (pmatch[0].rm_eo > 0) ? pmatch[0].rm_eo : 1;
+                    p += pmatch[0].rm_eo;
+                    if (pmatch[0].rm_eo - pmatch[0].rm_so == 0)
+                        {
+                        if (!tokenbuf_append(&tmp, p, 1))
+                            {
+                            regfree(&preg);
+                            tokenbuf_free(&tmp);
+                            tokenbuf_free(&mydata);
+                            tokenbuf_free(&myreplace);
+                            return VAR_ERR_OUT_OF_MEMORY;
+                            }
+                        ++p;
+                        }
                     tokenbuf_free(&myreplace);
                 }
                 if (!global) {
-                    tokenbuf_append(&tmp, p, mydata.end - p);
+                    if (!tokenbuf_append(&tmp, p, mydata.end - p)) {
+                        regfree(&preg);
+                        tokenbuf_free(&tmp);
+                        tokenbuf_free(&mydata);
+                        return VAR_ERR_OUT_OF_MEMORY;
+                    }
                     break;
                 }
             }

CVSTrac 2.0.1