-
Notifications
You must be signed in to change notification settings - Fork 197
ENT-3417: Added allow_non_convergent to pattern_matching promise in edit_line #6025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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]; | ||
|
|
@@ -1318,9 +1319,14 @@ static bool ReplacePatterns(EvalContext *ctx, Item *file_start, Item *file_end, | |
| // TODO: gripe if that truncated ! | ||
|
|
||
| // Substitute into line_buff: | ||
| snprintf(line_buff + start_off, sizeof(line_buff) - start_off, | ||
| int needed = snprintf(line_buff + start_off, sizeof(line_buff) - start_off, | ||
| "%s%s", BufferData(replace), after); | ||
| // TODO: gripe if that truncated or failed ! | ||
| if (needed < 0 || needed >= sizeof(line_buff) - start_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; | ||
|
|
||
|
|
@@ -1330,17 +1336,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
+1342
to
+1343
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check for truncation
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not resolved |
||
| int needed = snprintf(line_buff_cp + start_off, sizeof(line_buff_cp) - start_off, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| "%s%s", BufferData(replace), after); | ||
victormlg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (needed < 0 || needed >= sizeof(line_buff_cp) - start_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; | ||
| } | ||
|
|
||
| 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, | ||
|
|
@@ -1366,8 +1387,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); | ||
victormlg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| 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" | ||
|
|
||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you are including |
||
| { | ||
| 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"; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.