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
49 changes: 39 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,32 @@ 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));
Comment on lines +1337 to +1338
Copy link
Contributor

Choose a reason for hiding this comment

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

Please check for truncation

int needed = snprintf(line_buff_cp + start_off, sizeof(line_buff_cp) - start_off,
"%s%s", BufferData(replace), after);

if (needed >= sizeof(line_buff_cp) - start_off) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make sure needed is not a negative integer before comparing it with an unsigned

RecordInterruption(ctx, pp, a, "Buffer overflow: replacement string is too large. '%s' in '%s'",
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe avoid printing buffer overflow. Because you are avoiding it. The remaining message is sufficient in my opinion

a->column.column_separator, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
break;
}

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 +1382,21 @@ 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));
int needed = snprintf(line_buff_cp + start_off, sizeof(line_buff_cp) - start_off,
"%s%s", BufferData(replace), after);

if (needed >= sizeof(line_buff_cp) - start_off) {
RecordInterruption(ctx, pp, a, "Buffer overflow: replacement string is too large. '%s' in '%s'",
a->column.column_separator, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
break;
}

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
69 changes: 69 additions & 0 deletions tests/acceptance/10_files/replace_patterns/allow_non_convergent.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#######################################################
#
# Replace a pattern using non-convergent regexes
#
#######################################################

body common control
{
inputs => { "../../default.cf.sub" };
bundlesequence => { default("$(this.promise_filename)") };
version => "1.0";
}

######################################################

bundle agent init
{
files:
"/tmp/example.txt"
content => "foo PORT=23 bar";

}

######################################################

bundle agent test
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you are including default.cf.sub you can add the meta data with ticket number and description

{
files:
"/tmp/example.txt"
edit_line => _regex_replace( "PORT=[0-9]+", "PORT=22" );
}

bundle edit_line _regex_replace(find,replace)
{
replace_patterns:
"$(find)"
replace_with => _value("$(replace)"),
comment => "Search and replace string",
allow_non_convergent => "true";
}

body replace_with _value(x)
{
replace_value => "$(x)";
occurrences => "all";
}

######################################################

bundle agent check
{
vars:
"file_content"
string => readfile( "/tmp/example.txt" , "999" );

classes:
"ok" expression => strcmp("$(file_content)", "foo PORT=22 bar");

files:
"/tmp/example.txt"
delete => tidy;

reports:
ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
}

Loading