From: Chris Allegretta Date: Wed, 18 Feb 2004 04:33:53 +0000 (+0000) Subject: DLR and DB's latest fixes X-Git-Tag: v1.2.3~1 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=fbc8b99451aadcdcfc42c849a7438820fb1c13e4;p=nano.git DLR and DB's latest fixes git-svn-id: svn://svn.savannah.gnu.org/nano/branches/nano_1_2_branch/nano@1653 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 04f58f0e..f1e5cf7b 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,3 @@ -CVS code - - General: - Translation updates (see po/ChangeLog for details). - Make sure the "historylog" option isn't included at all if @@ -31,14 +30,17 @@ CVS code - - search.c: findnextstr(), do_replace_loop() - Fix potential infinite loops and other misbehavior when doing - certain zero-length regex replacements ("^", "$", and "^$"). - Add a no_sameline parameter to findnextstr(), and set it in - the calls in do_replace_loop() when such regexes are found, so - that such regexes are only found once per line. Also change - length_change from a long to an int; size_t is unsuitable due - to its being unsigned. (DLR; found by Mike Frysinger and DLR, - match_len by David Benbennick) David Benbennick: Add a few - minor cleanups to do_replace_loop(). + beginning-of-line or end-of-line regex replacements ("^", "$", + and "^$"). Add a no_sameline parameter to findnextstr(), and + set it in the calls in do_replace_loop() when such regexes are + found, so that such regexes are only found once per line. + Also change length_change from a long to an int; size_t is + unsuitable due to its being unsigned. (DLR; found by Mike + Frysinger and DLR, match_len by David Benbennick) David + Benbennick: Add a few minor cleanups to do_replace_loop(), and + fix segfault when doing a regex replace of a string that + matches inside a line (e.g. replace the "b" in "abc" with + anything). - winio.c: titlebar() - Fix problem with the available space for a filename on the diff --git a/color.c b/color.c index e8aacced..b37ee3ca 100644 --- a/color.c +++ b/color.c @@ -109,7 +109,7 @@ void update_color(void) for (e = tmpsyntax->extensions; e != NULL; e = e->next) { /* Set colorstrings if we matched the extension regex */ - if (!regexec(&e->val, filename, 0, NULL, 0)) + if (regexec(&e->val, filename, 0, NULL, 0) == 0) colorstrings = tmpsyntax->color; if (colorstrings != NULL) diff --git a/search.c b/search.c index 2abc4dcf..b9030ff4 100644 --- a/search.c +++ b/search.c @@ -573,8 +573,8 @@ int do_replace_loop(const char *prevanswer, const filestruct *begin, { int replaceall = 0, numreplaced = -1; #ifdef HAVE_REGEX_H - /* The starting-line match and zero-length regex flags. */ - int beginline = 0, caretdollar = 0; + /* The starting-line match and bol/eol regex flags. */ + int beginline = 0, bol_eol = 0; #endif filestruct *fileptr = NULL; @@ -604,31 +604,30 @@ int do_replace_loop(const char *prevanswer, const filestruct *begin, fileptr = findnextstr(fileptr || replaceall || search_last_line, FALSE, begin, *beginx, prevanswer, #ifdef HAVE_REGEX_H - /* We should find a zero-length regex only once per - * line. If the caretdollar flag is set, it means that - * the last search found one on the beginning line, so we + /* We should find a bol and/or eol regex only once per + * line. If the bol_eol flag is set, it means that the + * last search found one on the beginning line, so we * should skip over the beginning line when doing this * search. */ - caretdollar + bol_eol #else 0 #endif ); #ifdef HAVE_REGEX_H - /* If the caretdollar flag is set, we've found a match on the + /* If the bol_eol flag is set, we've found a match on the * beginning line already, and we're still on the beginning line * after the search, it means that we've wrapped around, so * we're done. */ - if (caretdollar && beginline && fileptr == begin) + if (bol_eol && beginline && fileptr == begin) fileptr = NULL; /* Otherwise, set the beginline flag if we've found a match on - * the beginning line, reset the caretdollar flag, and - * continue. */ + * the beginning line, reset the bol_eol flag, and continue. */ else { if (fileptr == begin) beginline = 1; - caretdollar = 0; + bol_eol = 0; } #endif @@ -669,10 +668,10 @@ int do_replace_loop(const char *prevanswer, const filestruct *begin, } #ifdef HAVE_REGEX_H - /* Set the caretdollar flag if we're doing a zero-length regex - * replace (such as "^", "$", or "^$"). */ - if (ISSET(USE_REGEXP) && match_len == 0) - caretdollar = 1; + /* Set the bol_eol flag if we're doing a bol and/or eol regex + * replace ("^", "$", or "^$"). */ + if (ISSET(USE_REGEXP) && regexec(&search_regexp, prevanswer, 0, NULL, REG_NOTBOL | REG_NOTEOL) == REG_NOMATCH) + bol_eol = 1; #endif if (*i > 0 || replaceall) { /* Yes, replace it!!!! */ diff --git a/utils.c b/utils.c index f18f86a3..1cf02673 100644 --- a/utils.c +++ b/utils.c @@ -197,13 +197,13 @@ const char *strstrwrapper(const char *haystack, const char *needle, #ifndef NANO_SMALL if (ISSET(REVERSE_SEARCH)) { /* When doing a backwards search, haystack is a whole line. */ - if (!regexec(&search_regexp, haystack, 1, regmatches, 0) && + if (regexec(&search_regexp, haystack, 1, regmatches, 0) == 0 && haystack + regmatches[0].rm_so <= rev_start) { const char *retval = haystack + regmatches[0].rm_so; /* Search forward until there is no more match. */ - while (!regexec(&search_regexp, retval + 1, 1, regmatches, - REG_NOTBOL) && + while (regexec(&search_regexp, retval + 1, 1, regmatches, + REG_NOTBOL) == 0 && retval + 1 + regmatches[0].rm_so <= rev_start) retval += 1 + regmatches[0].rm_so; /* Finally, put the subexpression matches in global @@ -214,8 +214,8 @@ const char *strstrwrapper(const char *haystack, const char *needle, } } else #endif /* !NANO_SMALL */ - if (!regexec(&search_regexp, haystack, 10, regmatches, - line_pos > 0 ? REG_NOTBOL : 0)) { + if (regexec(&search_regexp, haystack, 10, regmatches, + line_pos > 0 ? REG_NOTBOL : 0) == 0) { const char *retval = haystack + regmatches[0].rm_so; regexec(&search_regexp, retval, 10, regmatches, 0);