From: Bruno Haible Date: Mon, 24 May 2010 09:17:32 +0000 (+0200) Subject: Code cleanup. X-Git-Tag: v0.18.1~26 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c48a91427bd758c9ae9f530c64da3aa504d0d487;p=thirdparty%2Fgettext.git Code cleanup. --- diff --git a/gettext-tools/libgrep/ChangeLog b/gettext-tools/libgrep/ChangeLog index 0e3951701..7e30fc36d 100644 --- a/gettext-tools/libgrep/ChangeLog +++ b/gettext-tools/libgrep/ChangeLog @@ -1,3 +1,9 @@ +2010-05-24 Bruno Haible + + Code cleanup. + * m-regex.c (EGexecute): Write logic in a similar way as + m-fgrep.c:Fexecute. + 2010-05-24 Bruno Haible Fix bug: Avoid out-of-bounds access. diff --git a/gettext-tools/libgrep/m-regex.c b/gettext-tools/libgrep/m-regex.c index e3582bcb4..d5d3dd14e 100644 --- a/gettext-tools/libgrep/m-regex.c +++ b/gettext-tools/libgrep/m-regex.c @@ -179,52 +179,58 @@ EGexecute (const void *compiled_pattern, *match_size = len; return start; } - if ((!cregex->match_lines && !cregex->match_words) - || (cregex->match_lines && len == end - beg)) + if (cregex->match_lines) + { + if (len == end - beg) /* implies start == 0 */ + goto success; + } + else if (cregex->match_words) + { + /* If -w, check if the match aligns with word boundaries. + We do this iteratively because: + (a) the line may contain more than one occurence of the + pattern, and + (b) Several alternatives in the pattern might be valid at + a given point, and we may need to consider a shorter + one to find a word boundary. */ + while (start >= 0) + { + if ((start == 0 || !IS_WORD_CONSTITUENT ((unsigned char) beg[start - 1])) + && (start + len == end - beg + || !IS_WORD_CONSTITUENT ((unsigned char) beg[start + len]))) + goto success; + if (len > 0) + { + /* Try a shorter length anchored at the same place. */ + --len; + cregex->patterns[i].regexbuf.not_eol = 1; + len = re_match (&cregex->patterns[i].regexbuf, beg, + start + len, start, + &cregex->patterns[i].regs); + } + if (len <= 0) + { + /* Try looking further on. */ + if (start == end - beg) + break; + ++start; + cregex->patterns[i].regexbuf.not_eol = 0; + start = re_search (&cregex->patterns[i].regexbuf, beg, + end - beg, + start, end - beg - start, + &cregex->patterns[i].regs); + len = cregex->patterns[i].regs.end[0] - start; + } + } + } + else goto success; - /* If -w, check if the match aligns with word boundaries. - We do this iteratively because: - (a) the line may contain more than one occurence of the - pattern, and - (b) Several alternatives in the pattern might be valid at a - given point, and we may need to consider a shorter one to - find a word boundary. */ - if (cregex->match_words) - while (start >= 0) - { - if ((start == 0 || !IS_WORD_CONSTITUENT ((unsigned char) beg[start - 1])) - && (start + len == end - beg - || !IS_WORD_CONSTITUENT ((unsigned char) beg[start + len]))) - goto success; - if (len > 0) - { - /* Try a shorter length anchored at the same place. */ - --len; - cregex->patterns[i].regexbuf.not_eol = 1; - len = re_match (&cregex->patterns[i].regexbuf, beg, - start + len, start, - &cregex->patterns[i].regs); - } - if (len <= 0) - { - /* Try looking further on. */ - if (start == end - beg) - break; - ++start; - cregex->patterns[i].regexbuf.not_eol = 0; - start = re_search (&cregex->patterns[i].regexbuf, beg, - 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; success: