diff options
author | Bruno Haible <bruno@clisp.org> | 2010-05-24 11:17:32 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2010-06-03 15:03:48 +0200 |
commit | c48a91427bd758c9ae9f530c64da3aa504d0d487 (patch) | |
tree | 3d32a5be4868875500bd242bcaf0a4a7c0a0d808 /gettext-tools/libgrep | |
parent | eaa93942902485744c1ae0724a68efb2d814cb2d (diff) | |
download | external_gettext-c48a91427bd758c9ae9f530c64da3aa504d0d487.zip external_gettext-c48a91427bd758c9ae9f530c64da3aa504d0d487.tar.gz external_gettext-c48a91427bd758c9ae9f530c64da3aa504d0d487.tar.bz2 |
Code cleanup.
Diffstat (limited to 'gettext-tools/libgrep')
-rw-r--r-- | gettext-tools/libgrep/ChangeLog | 6 | ||||
-rw-r--r-- | gettext-tools/libgrep/m-regex.c | 88 |
2 files changed, 53 insertions, 41 deletions
diff --git a/gettext-tools/libgrep/ChangeLog b/gettext-tools/libgrep/ChangeLog index 0e39517..7e30fc3 100644 --- a/gettext-tools/libgrep/ChangeLog +++ b/gettext-tools/libgrep/ChangeLog @@ -1,5 +1,11 @@ 2010-05-24 Bruno Haible <bruno@clisp.org> + Code cleanup. + * m-regex.c (EGexecute): Write logic in a similar way as + m-fgrep.c:Fexecute. + +2010-05-24 Bruno Haible <bruno@clisp.org> + Fix bug: Avoid out-of-bounds access. * m-regex.c (EGexecute): Fix test before second call to IS_WORD_CONSTITUENT. diff --git a/gettext-tools/libgrep/m-regex.c b/gettext-tools/libgrep/m-regex.c index e3582bc..d5d3dd1 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: |