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
2 changes: 1 addition & 1 deletion cf-agent/cf-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -1547,7 +1547,7 @@ static void KeepPromiseBundles(EvalContext *ctx, const Policy *policy, GenericAg
if (bp)
{
BundleBanner(bp,args);
EvalContextStackPushBundleFrame(ctx, bp, args, false);
EvalContextStackPushBundleFrame(ctx, bp, args, false, NULL);
ScheduleAgentOperations(ctx, bp);
EvalContextStackPopFrame(ctx);
EndBundleBanner(bp);
Expand Down
6 changes: 3 additions & 3 deletions cf-agent/verify_files.c
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ static PromiseResult RenderTemplateCFEngine(EvalContext *ctx,

a.haveeditline = true;

EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit);
EvalContextStackPushBundleFrame(ctx, bp, bundle_args, a.edits.inherit, NULL);
BundleResolve(ctx, bp);

*save_file = ScheduleEditLineOperations(ctx, bp, &a, pp, edcontext);
Expand Down Expand Up @@ -1154,7 +1154,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
const Bundle *bp = EvalContextResolveBundleExpression(ctx, policy, edit_bundle_name, "edit_line");
if (bp)
{
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit);
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit, NULL);

BundleResolve(ctx, bp);

Expand Down Expand Up @@ -1192,7 +1192,7 @@ PromiseResult ScheduleEditOperation(EvalContext *ctx, char *filename,
const Bundle *bp = EvalContextResolveBundleExpression(ctx, policy, edit_bundle_name, "edit_xml");
if (bp)
{
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit);
EvalContextStackPushBundleFrame(ctx, bp, args, a->edits.inherit, NULL);
BundleResolve(ctx, bp);

ScheduleEditXmlOperations(ctx, bp, a, pp, edcontext);
Expand Down
2 changes: 1 addition & 1 deletion cf-agent/verify_methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@
{
BundleBanner(bp, args);
EvalContextSetBundleArgs(ctx, args);
EvalContextStackPushBundleFrame(ctx, bp, args, a->inherit);
EvalContextStackPushBundleFrame(ctx, bp, args, a->inherit, PromiseGetBundle(pp));

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter a in VerifyMethod() is dereferenced without an explicit null-check

/* Clear all array-variables that are already set in the sub-bundle.
Otherwise, array-data accumulates between multiple bundle evaluations.
Expand Down
2 changes: 1 addition & 1 deletion cf-monitord/env_monitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,7 +1143,7 @@ static void GatherPromisedMeasures(EvalContext *ctx, const Policy *policy)
for (size_t i = 0; i < SeqLength(policy->bundles); i++)
{
const Bundle *bp = SeqAt(policy->bundles, i);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);

if ((strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_MONITOR]) == 0) || (strcmp(bp->type, CF_AGENTTYPES[AGENT_TYPE_COMMON]) == 0))
{
Expand Down
2 changes: 1 addition & 1 deletion cf-serverd/server_transform.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,7 +641,7 @@ static bool IsPromiseTypeNotInTypeSequence(const char *promise_type,

static void EvaluateBundle(EvalContext *ctx, const Bundle *bp, const char * const *seq)
{
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);

for (int type = 0; seq[type] != NULL; type++)
{
Expand Down
15 changes: 12 additions & 3 deletions libpromises/eval_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -1362,13 +1362,14 @@ static StackFrame *StackFrameNew(StackFrameType type, bool inherit_previous)
return frame;
}

static StackFrame *StackFrameNewBundle(const Bundle *owner, bool inherit_previous, bool profiling, EventFrame *prev_event)
static StackFrame *StackFrameNewBundle(const Bundle *owner, bool inherit_previous, bool profiling, const Bundle *calling_bundle, EventFrame *prev_event)
{
StackFrame *frame = StackFrameNew(STACK_FRAME_TYPE_BUNDLE, inherit_previous);

frame->data.bundle.owner = owner;
frame->data.bundle.classes = ClassTableNew();
frame->data.bundle.vars = VariableTableNew();
frame->data.bundle.calling_bundle = calling_bundle;
frame->event = (profiling) ? BundleToEventFrame(owner, prev_event) : NULL;

return frame;
Expand Down Expand Up @@ -1445,12 +1446,12 @@ static void EvalContextStackPushFrame(EvalContext *ctx, StackFrame *frame)
STACK_FRAME_TYPE_STR[frame->type]);
}

void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous)
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous, const Bundle *calling_bundle)
{
assert(ctx != NULL);
assert(!LastStackFrame(ctx, 0) || LastStackFrame(ctx, 0)->type == STACK_FRAME_TYPE_PROMISE_ITERATION);

StackFrame *frame = StackFrameNewBundle(owner, inherits_previous, ctx->profiling, EvalContextGetLastEventFrame(ctx));
StackFrame *frame = StackFrameNewBundle(owner, inherits_previous, ctx->profiling, calling_bundle, EvalContextGetLastEventFrame(ctx));
EvalContextStackPushFrame(ctx, frame);

if (RlistLen(args) > 0)
Expand Down Expand Up @@ -1574,6 +1575,14 @@ void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner)
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "bundle", PromiseGetBundle(owner)->name, CF_DATA_TYPE_STRING, "source=promise");
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "namespace", PromiseGetNamespace(owner), CF_DATA_TYPE_STRING, "source=promise");

StackFrame *bundle_frame = LastStackFrameByType(ctx, STACK_FRAME_TYPE_BUNDLE);
if (bundle_frame != NULL && bundle_frame->data.bundle.calling_bundle != NULL)
{
char *calling_bundle_name = StringFormat("%s:%s", bundle_frame->data.bundle.calling_bundle->ns, bundle_frame->data.bundle.calling_bundle->name);
EvalContextVariablePutSpecial(ctx, SPECIAL_SCOPE_THIS, "calling_bundle", calling_bundle_name, CF_DATA_TYPE_STRING, "source=promise");
free(calling_bundle_name);
}

// Recompute `with`
for (size_t i = 0; i < SeqLength(owner->conlist); i++)
{
Expand Down
3 changes: 2 additions & 1 deletion libpromises/eval_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ typedef enum
typedef struct
{
const Bundle *owner;
const Bundle *calling_bundle;

ClassTable *classes;
VariableTable *vars;
Expand Down Expand Up @@ -187,7 +188,7 @@ void EvalContextClear(EvalContext *ctx);

Rlist *EvalContextGetPromiseCallerMethods(EvalContext *ctx);

void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous);
void EvalContextStackPushBundleFrame(EvalContext *ctx, const Bundle *owner, const Rlist *args, bool inherits_previous, const Bundle *calling_bundle);
void EvalContextStackPushBodyFrame(EvalContext *ctx, const Promise *caller, const Body *body, const Rlist *args);
void EvalContextStackPushBundleSectionFrame(EvalContext *ctx, const BundleSection *owner);
void EvalContextStackPushPromiseFrame(EvalContext *ctx, const Promise *owner);
Expand Down
4 changes: 2 additions & 2 deletions libpromises/expand.c
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ void PolicyResolve(EvalContext *ctx, const Policy *policy,
Bundle *bundle = SeqAt(policy->bundles, i);
if (strcmp("common", bundle->type) == 0)
{
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
BundleResolve(ctx, bundle); /* PRE-EVAL classes,vars */
EvalContextStackPopFrame(ctx);
}
Expand All @@ -1136,7 +1136,7 @@ void PolicyResolve(EvalContext *ctx, const Policy *policy,
Bundle *bundle = SeqAt(policy->bundles, i);
if (strcmp("common", bundle->type) != 0)
{
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
BundleResolve(ctx, bundle); /* PRE-EVAL vars */
EvalContextStackPopFrame(ctx);
}
Expand Down
2 changes: 1 addition & 1 deletion libpromises/loading.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ Policy *LoadPolicy(EvalContext *ctx, GenericAgentConfig *config)
for (size_t i = 0; i < SeqLength(policy->bundles); i++)
{
Bundle *bp = SeqAt(policy->bundles, i);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);

for (size_t j = 0; j < SeqLength(bp->sections); j++)
{
Expand Down
65 changes: 65 additions & 0 deletions tests/acceptance/01_vars/01_basic/calling_bundle.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Test $(this.calling_bundle)

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

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

bundle agent init
{
methods:
"a"
usebundle => new_namespace:A,
useresult => "return_var";
}

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

bundle agent check
{
vars:
"expected"
string => "default:init/new_namespace:A/B";

classes:
"ok"
expression => strcmp("$(init.return_var[1])", "$(expected)");

reports:
DEBUG::
"$(this.promise_filename) expected dirname $(init.return_var[1]), actual $(expected)";

ok::
"$(this.promise_filename) Pass";
!ok::
"$(this.promise_filename) FAIL";
}

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


bundle agent A
{
methods:
"b"
usebundle => new_namespace:B,
useresult => "return_var";

reports:
"$(this.calling_bundle)/$(return_var[1])"
bundle_return_value_index => "1";

}

bundle agent B
{
reports:
"$(this.calling_bundle)/$(this.bundle)"
bundle_return_value_index => "1";
}
2 changes: 1 addition & 1 deletion tests/unit/eval_context_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static void test_class_persistence(void)
Policy *p = PolicyNew();
Bundle *bp = PolicyAppendBundle(p, "ns1", "bundle1", "agent", NULL, NULL, EVAL_ORDER_UNDEFINED);

EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);
EvalContextHeapPersistentSave(ctx, "class2", 5, CONTEXT_STATE_POLICY_PRESERVE, "x");
EvalContextStackPopFrame(ctx);

Expand Down
10 changes: 5 additions & 5 deletions tests/unit/expand_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ static void test_map_iterators_from_rval_naked_list_var(void **state)
RlistDestroy(list);
}

EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);

{
Rlist *lists = NULL;
Expand Down Expand Up @@ -203,7 +203,7 @@ static void test_map_iterators_from_rval_naked_list_var_namespace(void **state)
RlistDestroy(list);
}

EvalContextStackPushBundleFrame(ctx, bp, NULL, false);
EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL);

{
Rlist *lists = NULL;
Expand Down Expand Up @@ -425,7 +425,7 @@ static void test_expand_promise_array_with_scalar_arg(void **state)
BundleSection *section = BundleAppendSection(bundle, "dummy");
Promise *promise = BundleSectionAppendPromise(section, "$(foo[$(bar)])", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);

EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
EvalContextStackPushBundleSectionFrame(ctx, section);
ExpandPromise(ctx, promise, actuator_expand_promise_array_with_scalar_arg, NULL);
EvalContextStackPopFrame(ctx);
Expand Down Expand Up @@ -480,7 +480,7 @@ static void test_expand_promise_slist(void **state)
BundleSection *section = BundleAppendSection(bundle, "dummy");
Promise *promise = BundleSectionAppendPromise(section, "$(foo)", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);

EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
EvalContextStackPushBundleSectionFrame(ctx, section);
ExpandPromise(ctx, promise, actuator_expand_promise_slist, NULL);
EvalContextStackPopFrame(ctx);
Expand Down Expand Up @@ -548,7 +548,7 @@ static void test_expand_promise_array_with_slist_arg(void **state)
BundleSection *section = BundleAppendSection(bundle, "dummy");
Promise *promise = BundleSectionAppendPromise(section, "$(arr[$(keys)])", (Rval) { NULL, RVAL_TYPE_NOPROMISEE }, "any", NULL);

EvalContextStackPushBundleFrame(ctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(ctx, bundle, NULL, false, NULL);
EvalContextStackPushBundleSectionFrame(ctx, section);
ExpandPromise(ctx, promise, actuator_expand_promise_array_with_slist_arg, NULL);
EvalContextStackPopFrame(ctx);
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/iteration_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static void IteratorPrepare_TestHelper(
Promise *promise = BundleSectionAppendPromise(section, promiser,
(Rval) { NULL, RVAL_TYPE_NOPROMISEE },
"any", NULL);
EvalContextStackPushBundleFrame(evalctx, bundle, NULL, false);
EvalContextStackPushBundleFrame(evalctx, bundle, NULL, false, NULL);
EvalContextStackPushBundleSectionFrame(evalctx, section);
PromiseIterator *iterctx = PromiseIteratorNew(promise);
char *promiser_copy = xstrdup(promiser);
Expand Down
Loading