core: fix string evaluation with regex replacement when the string is empty

v2.8-utf8proc
Sébastien Helleu 2018-08-16 22:45:42 +02:00
parent ca90f28beb
commit 1a0087a7d7
3 changed files with 35 additions and 10 deletions

View File

@ -25,6 +25,7 @@ New features::
Bug fixes::
* core: fix string evaluation with regex replacement when the string is empty
* core: fix check of tags in lines (command /filter and hook_print)
* core: fix clear of completion item in case of partial completion (issue #1162)
* core: send signal "key_pressed" for mouse code only if the string is UTF-8 valid (issue #1220)

View File

@ -1100,6 +1100,7 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace,
{
char *result, *result2, *str_replace;
int length, length_replace, start_offset, i, rc, end;
int empty_replace_allowed;
struct t_eval_regex eval_regex;
if (!string || !regex || !replace)
@ -1114,7 +1115,11 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace,
eval_context->regex = &eval_regex;
start_offset = 0;
while (result && result[start_offset])
/* we allow one empty replace if input string is empty */
empty_replace_allowed = (result[0]) ? 0 : 1;
while (result)
{
for (i = 0; i < 100; i++)
{
@ -1122,16 +1127,20 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace,
}
rc = regexec (regex, result + start_offset, 100, eval_regex.match, 0);
/*
* no match found: exit the loop (if rm_eo == 0, it is an empty match
* at beginning of string: we consider there is no match, to prevent an
* infinite loop)
*/
if ((rc != 0)
|| (eval_regex.match[0].rm_so < 0)
|| (eval_regex.match[0].rm_eo <= 0))
{
/* no match found: exit the loop */
if ((rc != 0) || (eval_regex.match[0].rm_so < 0))
break;
/*
* if empty string is matching, continue only if empty replace is
* still allowed (to prevent infinite loop)
*/
if (eval_regex.match[0].rm_eo <= 0)
{
if (!empty_replace_allowed)
break;
empty_replace_allowed = 0;
}
/* adjust the start/end offsets */
@ -1183,6 +1192,9 @@ eval_replace_regex (const char *string, regex_t *regex, const char *replace,
break;
start_offset = eval_regex.match[0].rm_so + length_replace;
if (!result[start_offset])
break;
}
return result;

View File

@ -452,6 +452,18 @@ TEST(CoreEval, EvalReplaceRegex)
NULL, NULL);
CHECK(options);
/* replace regex by empty string (on empty string) */
hashtable_remove (pointers, "regex");
hashtable_set (options, "regex", ".*");
hashtable_set (options, "regex_replace", "");
WEE_CHECK_EVAL("", "");
/* replace regex (on empty string) */
hashtable_remove (pointers, "regex");
hashtable_set (options, "regex", ".*");
hashtable_set (options, "regex_replace", "test");
WEE_CHECK_EVAL("test", "");
/* replace regex by empty string */
hashtable_remove (pointers, "regex");
hashtable_set (options, "regex", ".*");