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

ENT-11923: Fixed bug in double expansion of foreign list variables with namespaces #5564

Merged
merged 1 commit into from
Jul 8, 2024
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
150 changes: 104 additions & 46 deletions libpromises/iteration.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,108 @@ static Seq *IterableToSeq(const void *v, DataType t)
}
}

/* During initialization of the iteration engine, lists from foreign bundles are
* mangled in order to avoid overwriting the values in that bundle. However, the
* iteration engine has no way of knowing that some variables like
* $($(parent_bundle).lst) is a list during initialization. Hence, while
* crunching the wheels, this hack makes sure foreign variables are mangled when
* ever they expand into a list.
*
* How does it work? It looks for a '.' in both #iterctx->pp->promiser and
* #varname, where the left side of #varname is a bundle name and the right side
* of #iterctx->pp->promiser begins with the right side of #varname. If all of
* the constraints are fulfilled it swaps the '.' with '#' in both variables.
* Furthermore, if the variable also contains a scope, it will mangle ':' with
* '*'.
*
* See ticket ENT-9491 & ENT-11923 for more info.
*/
static void WheelMangleAfterExpandingToList(const PromiseIterator *iterctx,
EvalContext *evalctx, char *varname)
{
assert(iterctx != NULL);
assert(iterctx->pp != NULL);
assert(iterctx->pp->promiser != NULL);

const StringSet *scopes = EvalContextGetBundleNames(evalctx);

// In case there is a namespace
char *unexp_namespace_token = iterctx->pp->promiser;
Fixed Show fixed Hide fixed
while ((unexp_namespace_token = strchr(unexp_namespace_token, CF_NS)) != NULL)
{
char *exp_namespace_token = varname;
while ((exp_namespace_token = strchr(exp_namespace_token, CF_NS)) != NULL)
{
char *unexp_scope_token = iterctx->pp->promiser;
Fixed Show fixed Hide fixed
while ((unexp_scope_token = strchr(unexp_scope_token, '.')) != NULL)
{
char *exp_scope_token = varname;
while ((exp_scope_token = strchr(exp_scope_token, '.')) != NULL)
{
if (exp_scope_token < exp_namespace_token)
{
// A scope token cannot come before a namespace token
continue;
}

*exp_scope_token = '\0';
const char *exp_scope = exp_namespace_token + 1;
const char *unexp_name = unexp_scope_token + 1;
const char *exp_name = exp_scope_token + 1;

if (StringStartsWith(unexp_name, exp_name) &&
StringSetContains(scopes, exp_scope))
{
*unexp_namespace_token = CF_MANGLED_NS;
*exp_namespace_token = CF_MANGLED_NS;
*unexp_scope_token = CF_MANGLED_SCOPE;
*exp_scope_token = CF_MANGLED_SCOPE;

// We are done!
return;
}

// Put things back the way they were
*exp_scope_token = '.';
exp_scope_token += 1;
}
unexp_scope_token += 1;
}
exp_namespace_token += 1;
}
unexp_namespace_token += 1;
}

// In case there is no namespace
char *unexp_scope_token = iterctx->pp->promiser;
Fixed Show fixed Hide fixed
while ((unexp_scope_token = strchr(unexp_scope_token, '.')) != NULL)
{
char *exp_scope_token = varname;
while ((exp_scope_token = strchr(exp_scope_token, '.')) != NULL)
{
*exp_scope_token = '\0';
const char *exp_scope = varname;
const char *unexp_name = unexp_scope_token + 1 ;
const char *exp_name = exp_scope_token + 1;

if (StringStartsWith(unexp_name, exp_name) &&
StringSetContains(scopes, exp_scope))
{
*unexp_scope_token = CF_MANGLED_SCOPE;
*exp_scope_token = CF_MANGLED_SCOPE;

// We are done!
return;
}

// Put things back the way they were
*exp_scope_token = '.';
exp_scope_token += 1;
}
unexp_scope_token += 1;
}
}

/**
* For each of the wheels to the right of wheel_idx (including this one)
*
Expand Down Expand Up @@ -862,54 +964,10 @@ static void ExpandAndPutWheelVariablesAfter(
assert(SeqLength(wheel->values) > 0);
assert( SeqAt(wheel->values, 0) != NULL);

/* During initialization of the iteration engine, lists from
* foreign bundles are mangled in order to avoid overwriting
* the values in that bundle. However, the iteration engine
* has no way of knowing that some variables like
* $($(parent_bundle).lst) is a list during initialization.
* Hence, while crunching the wheels, this hack makes sure
* foreign variables are mangled when ever they expand into
* a list.
*
* How does it work? It looks for a '.' in both
* #iterctx->pp->promiser and #varname, where the left side
* of #varname is a bundle name and the right side of
* #iterctx->pp->promiser begins with the right side of
* #varname. If all of the constraints are fulfilled it
* swaps the '.' with '#' in both variables.
*
* See ticket ENT-9491 for more info.
*/
if (!IsMangled(varname))
{
const StringSet *scopes = EvalContextGetBundleNames(evalctx);

bool done = false;
char *unexp_pos = iterctx->pp->promiser;
while (!done && (unexp_pos = strchr(unexp_pos, '.')) != NULL)
{
char *exp_pos = (char *)varname;
while (!done && (exp_pos = strchr(exp_pos, '.')) != NULL)
{
if (StringStartsWith(unexp_pos + 1, exp_pos + 1))
{
char tmp = *exp_pos;
*exp_pos = '\0';
if (StringSetContains(scopes, varname))
{
*exp_pos = CF_MANGLED_SCOPE;
*unexp_pos = CF_MANGLED_SCOPE;
done = true;
}
else
{
*exp_pos = tmp;
}
}
exp_pos += 1;
}
unexp_pos += 1;
}
WheelMangleAfterExpandingToList(iterctx, evalctx,
(char *) varname);
}

/* Put the first value of the iterable. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
##############################################################################
#
# Test double expansion of list from remote bundle with namespace (ENT-11923)
#
##############################################################################

body common control
{
bundlesequence => { "bogus:check" };
}

bundle agent test(parent_namespace, parent_bundle)
{
meta:
"description" -> { "ENT-11923" }
string => "Test double expension of list from remote bundle with namespace";

reports:
"$($(parent_namespace):$(parent_bundle).str)"
comment => "Double expansion of remote string works prior to fix",
bundle_return_value_index => "foo";

"$($(parent_namespace):$(parent_bundle).lst)"
comment => "But double expansion of remote list does not work prior to fix",
bundle_return_value_index => "bar";

"$(bogus:check.lst)"
comment => "Single expansion of remote list works prior to fix",
bundle_return_value_index => "baz";
}

body file control
{
namespace => "bogus";
}

bundle agent check
{
vars:
"str"
string => "EXPANDED";
"lst"
slist => { "EXPANDED" };

methods:
"holder"
usebundle => default:test("$(this.namespace)", "$(this.bundle)"),
useresult => "ret";

reports:
"$(this.promise_filename) Pass"
if => and(strcmp("$(ret[foo])", "EXPANDED"),
strcmp("$(ret[bar])", "EXPANDED"),
strcmp("$(ret[baz])", "EXPANDED"));

"$(this.promise_filename) FAIL"
unless => and(strcmp("$(ret[foo])", "EXPANDED"),
strcmp("$(ret[bar])", "EXPANDED"),
strcmp("$(ret[baz])", "EXPANDED"));

default:DEBUG::
"$(const.dollar)($(const.dollar)(parent_namespace):$(const.dollar)(parent_bundle).str) => $(ret[foo])";
"$(const.dollar)($(const.dollar)(parent_namespace):$(const.dollar)(parent_bundle).lst) => $(ret[bar])";
"$(const.dollar)(default:check.lst) => $(ret[baz])";
}
Loading