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
94 changes: 76 additions & 18 deletions cf-agent/files_editline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,31 @@

/********************************************************************/

static bool ReplaceBufferSafeWrite(char *line_buff, char *after, Buffer *replace, int start_off, int end_off)
{
int needed;
// Save portion of line after substitution:
needed = strlcpy(after, line_buff + end_off, sizeof(after));

Check failure

Code scanning / CodeQL

Suspicious 'sizeof' use High

This evaluates to the size of the pointer type, which may not be what you want.
if (needed >= sizeof(after))

Check failure

Code scanning / CodeQL

Suspicious 'sizeof' use High

This evaluates to the size of the pointer type, which may not be what you want.
{
return false;
}
needed = snprintf(line_buff + start_off, sizeof(line_buff) - start_off,

Check failure

Code scanning / CodeQL

Suspicious 'sizeof' use High

This evaluates to the size of the pointer type, which may not be what you want.
"%s%s", BufferData(replace), after);
if (needed < 0 || (size_t) needed >= (sizeof(line_buff) - start_off))

Check failure

Code scanning / CodeQL

Suspicious 'sizeof' use High

This evaluates to the size of the pointer type, which may not be what you want.
{
return false;
}
return true;
}

static bool ReplacePatterns(EvalContext *ctx, Item *file_start, Item *file_end, const Attributes *a,
const Promise *pp, EditContext *edcontext, PromiseResult *result)
{
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 @@ -1313,14 +1332,13 @@
Log(LOG_LEVEL_VERBOSE, "Verifying replacement of '%s' with '%s', cutoff %d", pp->promiser, BufferData(replace),
cutoff);

// Save portion of line after substitution:
strlcpy(after, line_buff + end_off, sizeof(after));
// TODO: gripe if that truncated !

// Substitute into line_buff:
snprintf(line_buff + start_off, sizeof(line_buff) - start_off,
"%s%s", BufferData(replace), after);
// TODO: gripe if that truncated or failed !
if (!ReplaceBufferSafeWrite(line_buff, after, replace, start_off, end_off))
{
RecordInterruption(ctx, pp, a, "Replacement string is too large. '%s' in '%s'",
a->column.column_separator, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
break;
}
notfound = false;
replaced = true;

Expand All @@ -1330,17 +1348,40 @@
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;
int needed = strlcpy(line_buff_cp, line_buff, sizeof(line_buff_cp));
if (needed >= sizeof(line_buff_cp))
{
RecordInterruption(ctx, pp, a, "Original string is too large. '%s' in '%s'",
a->column.column_separator, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
break;
}

if (!ReplaceBufferSafeWrite(line_buff_cp, after, replace, 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);
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 +1407,25 @@
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))))
{
int needed = strlcpy(line_buff_cp, line_buff, sizeof(line_buff_cp));
if (needed >= sizeof(line_buff_cp))
{
RecordInterruption(ctx, pp, a, "Original string is too large. '%s' in '%s'",
a->column.column_separator, edcontext->filename);
*result = PromiseResultUpdate(*result, PROMISE_RESULT_INTERRUPTED);
break;
}

if (!ReplaceBufferSafeWrite(line_buff_cp, after, replace, start_off, end_off)) {
RecordInterruption(ctx, pp, a, "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
73 changes: 73 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,73 @@
#######################################################
#
# 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

{
meta:
"description" -> { "ENT-3417" }
string => "Test that allow_non_convergent correctly replaces non-convergent regex";

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