]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Bug fix: Don't assume that the buffer ends with a newline.
authorBruno Haible <bruno@clisp.org>
Sun, 23 May 2010 23:58:56 +0000 (01:58 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 3 Jun 2010 13:03:47 +0000 (15:03 +0200)
gettext-tools/libgrep/ChangeLog
gettext-tools/libgrep/m-fgrep.c
gettext-tools/libgrep/m-regex.c

index a1a407e861f02f34ad784bcecb140569f371bd96..1409a7e8f9b38876340f367bf869810752cdef03 100644 (file)
@@ -5,6 +5,15 @@
        * kwset.h: Likewise.
        * kwset.c: Likewise.
 
+2010-05-23  Bruno Haible  <bruno@clisp.org>
+
+       Fix bug: A regex was not regonized when it matched the end of the last
+       line and that last line was not terminated with a newline.
+       * m-regex.c (EGexecute): Don't ignore the last byte of the buffer if it
+       is not a newline.
+       * m-fgrep.c (Fexecute): Don't assume that the buffer contains a
+       newline.
+
 2010-05-23  Bruno Haible  <bruno@clisp.org>
 
        Do regex matching purely with regex, not regex + dfa + kwset.
index 23c4d85d3f680fabc569a4dad5e2b03c9b9e6d2c..1711cec67f1e7601dbc54a43212fd8f03bfd7516 100644 (file)
@@ -233,7 +233,10 @@ Fexecute (const void *compiled_pattern, const char *buf, size_t buf_size,
 
  success:
   end = (const char *) memchr (beg + len, eol, (buf + buf_size) - (beg + len));
-  end++;
+  if (end != NULL)
+    end++;
+  else
+    end = buf + buf_size;
   while (buf < beg && beg[-1] != eol)
     --beg;
   *match_size = end - beg;
index 5044fb78d10bd36b98a69ddaaad31c9e51016544..8af10dfdc5bb39884c15df6510fd672bba4f42f7 100644 (file)
@@ -155,17 +155,16 @@ EGexecute (const void *compiled_pattern,
   for (beg = buf; beg < buflim; beg = end)
     {
       end = (const char *) memchr (beg, eol, buflim - beg);
-      if (end != NULL)
-        end++;
-      else
+      if (end == NULL)
         end = buflim;
+      /* Here, either end < buflim && *end == eol, or end == buflim.  */
 
       for (i = 0; i < cregex->pcount; i++)
         {
-          cregex->patterns[i].regexbuf.not_eol = 0;
+          cregex->patterns[i].regexbuf.not_eol = (end == buflim);
           if (0 <= (start = re_search (&(cregex->patterns[i].regexbuf), beg,
-                                       end - beg - 1, 0,
-                                       end - beg - 1, &(cregex->patterns[i].regs))))
+                                       end - beg, 0,
+                                       end - beg, &(cregex->patterns[i].regs))))
             {
               len = cregex->patterns[i].regs.end[0] - start;
               if (exact)
@@ -174,7 +173,7 @@ EGexecute (const void *compiled_pattern,
                   return start;
                 }
               if ((!cregex->match_lines && !cregex->match_words)
-                  || (cregex->match_lines && len == end - beg - 1))
+                  || (cregex->match_lines && len == end - beg))
                 goto success;
               /* If -w, check if the match aligns with word boundaries.
                  We do this iteratively because:
@@ -187,7 +186,7 @@ EGexecute (const void *compiled_pattern,
                 while (start >= 0)
                   {
                     if ((start == 0 || !IS_WORD_CONSTITUENT ((unsigned char) beg[start - 1]))
-                        && (len == end - beg - 1
+                        && (len == end - beg
                             || !IS_WORD_CONSTITUENT ((unsigned char) beg[start + len])))
                       goto success;
                     if (len > 0)
@@ -202,19 +201,22 @@ EGexecute (const void *compiled_pattern,
                     if (len <= 0)
                       {
                         /* Try looking further on. */
-                        if (start == end - beg - 1)
+                        if (start == end - beg)
                           break;
                         ++start;
-                        cregex->patterns[i].regexbuf.not_eol = 0;
+                        cregex->patterns[i].regexbuf.not_eol = (end == buflim);
                         start = re_search (&(cregex->patterns[i].regexbuf), beg,
-                                           end - beg - 1,
-                                           start, end - beg - 1 - start,
+                                           end - beg,
+                                           start, end - beg - start,
                                            &(cregex->patterns[i].regs));
                         len = cregex->patterns[i].regs.end[0] - start;
                       }
                   }
             }
         } /* for Regex patterns.  */
+
+      if (end < buflim)
+        end++;
     } /* for (beg = end ..) */
   return (size_t) -1;