Skip to content

Commit

Permalink
Merge pull request #5330 from larsewi/amousset-3.21.x
Browse files Browse the repository at this point in the history
CFE-4254: Allow inheriting attributes using global variables in bodies having local parameters (3.21.x)
  • Loading branch information
larsewi authored Sep 22, 2023
2 parents 3f6dd0c + f72d430 commit df829de
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
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)");
}

0 comments on commit df829de

Please sign in to comment.