Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 25 additions & 10 deletions cf-agent/files_editline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ static bool ReplacePatterns(EvalContext *ctx, Item *file_start, Item *file_end,
assert(a != NULL);
assert(pp != NULL);
assert(edcontext != NULL);
bool allow_non_convergent = PromiseGetConstraintAsBoolean(ctx, "allow_non_convergent", pp);

char line_buff[CF_EXPANDSIZE];
char after[CF_BUFSIZE];
Expand Down Expand Up @@ -1330,17 +1331,25 @@ static bool ReplacePatterns(EvalContext *ctx, Item *file_start, Item *file_end,
break;
}
}

char line_buff_cp[CF_EXPANDSIZE];
if (NotAnchored(pp->promiser) && BlockTextMatch(ctx, pp->promiser, line_buff, &start_off, &end_off))
{
RecordInterruption(ctx, pp, a,
"Promised replacement '%s' on line '%s' for pattern '%s'"
" is not convergent while editing '%s'"
" (regular expression matches the replacement string)",
line_buff, ip->name, pp->promiser, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
PromiseRef(LOG_LEVEL_ERR, pp);
break;
strlcpy(line_buff_cp, line_buff, sizeof(line_buff_cp));
strlcpy(after, line_buff_cp + end_off, sizeof(after));
snprintf(line_buff_cp + start_off, sizeof(line_buff_cp) - start_off,
"%s%s", BufferData(replace), after);
Comment on lines +1337 to +1340
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if you could check for truncation here and emit an error message. If you are certain that a truncation will never occur, then you can at least add an assert to test this assumption.


if (!allow_non_convergent || (strlen(line_buff) != strlen(line_buff_cp)))
{
RecordInterruption(ctx, pp, a,
"Promised replacement '%s' on line '%s' for pattern '%s'"
" is not convergent while editing '%s'"
" (regular expression matches the replacement string)",
line_buff, ip->name, pp->promiser, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
PromiseRef(LOG_LEVEL_ERR, pp);
break;
}
}

if (!MakingChanges(ctx, pp, a, result, "replace pattern '%s' in '%s'", pp->promiser,
Expand All @@ -1366,8 +1375,14 @@ static bool ReplacePatterns(EvalContext *ctx, Item *file_start, Item *file_end,
break;
}

if (BlockTextMatch(ctx, pp->promiser, ip->name, &start_off, &end_off))
if (BlockTextMatch(ctx, pp->promiser, ip->name, &start_off, &end_off) && (!allow_non_convergent
|| (strlen(line_buff) != strlen(line_buff_cp))))
{
strlcpy(line_buff_cp, line_buff, sizeof(line_buff_cp));
strlcpy(after, line_buff_cp + end_off, sizeof(after));
snprintf(line_buff_cp + start_off, sizeof(line_buff_cp) - start_off,
"%s%s", BufferData(replace), after);
Comment on lines +1381 to +1384
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here


RecordInterruption(ctx, pp, a,
"Promised replacement '%s' for pattern '%s' is not properly convergent while editing '%s'"
" (pattern still matches the end-state replacement string '%s', consider use"
Expand Down
1 change: 1 addition & 0 deletions libpromises/mod_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ static const ConstraintSyntax CF_COLUMN_BODIES[] =
static const ConstraintSyntax CF_REPLACE_BODIES[] =
{
ConstraintSyntaxNewBody("replace_with", &replace_with_body, "Search-replace pattern", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewBool("allow_non_convergent", "Allow to use non-convergent regular expressions in replace_patterns. Defaults to false", SYNTAX_STATUS_NORMAL),
ConstraintSyntaxNewNull()
};

Expand Down
Loading