Skip to content
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

CFE-4254: Allow inheriting attributes using global variables in bodies having local parameters (3.21.x) #5330

Merged
merged 1 commit into from
Sep 22, 2023
Merged
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
14 changes: 12 additions & 2 deletions libpromises/rlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -373,13 +373,14 @@ Rval RvalNewRewriter(const void *item, RvalType type, JsonElement *map)

Buffer *format = BufferNew();
StringCopy(item, buffer_from, max_size);
buffer_to[0] = '\0';

for (int iteration = 0; iteration < 10; iteration++)
{
bool replacement_made = false;
int var_start = -1;
char closing_brace = 0;
for (int c = 0; c < buffer_from[c]; c++)
for (int c = 0; buffer_from[c] != '\0'; c++)
{
if (buffer_from[c] == '$')
{
Expand All @@ -402,6 +403,7 @@ Rval RvalNewRewriter(const void *item, RvalType type, JsonElement *map)
{
char saved = buffer_from[c];
buffer_from[c] = '\0';

const char *repl = JsonObjectGetAsString(map, buffer_from + var_start + 2);
buffer_from[c] = saved;

Expand Down Expand Up @@ -433,7 +435,15 @@ Rval RvalNewRewriter(const void *item, RvalType type, JsonElement *map)
}
}

char *ret = xstrdup(buffer_to);
char* ret;
if (buffer_to[0] == '\0') {
// If nothing has been written to buffer_to, we pass the input verbatim.
// This function only evaluates body parameters, but the body can also reference the global
// context.
ret = xstrdup(buffer_from);
} else {
ret = xstrdup(buffer_to);
}

BufferDestroy(format);
free(buffer_to);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#######################################################
#
# Test that bodies can inherit attributes containing global variables
#
#######################################################

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

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


bundle agent init
{
vars:
"class" string => "_pass";
}

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

body classes parent(p)
{
promise_kept => { "${init.class}" };
}

body classes static(p)
{
inherit_from => parent(p);
}

bundle agent test {
meta:
"description" -> { "CFE-4254" }
string => "Test that bodies can inherit attributes containing global variables";

vars:
"test" string => "test",
classes => static("placeholder");
}

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

bundle agent check
{
methods:
_pass::
"pass" usebundle => dcs_pass("$(this.promise_filename)");

!_pass::
"pass" usebundle => dcs_fail("$(this.promise_filename)");
}