Skip to content

Commit

Permalink
Redmine#7871: use VarRefValueToJson consistently and copy only when n…
Browse files Browse the repository at this point in the history
…eeded

VarRefValueToJson() was no longer used so
VarRefValueToJsonAllowScalars() was renamed to it.

Fixes a bug in VarRefValueToJsonAllowScalars() that leaked cf_nulls
(typo: used value instead of rp).

Fixes a bug in SeqShuffle() so it doesn't crash with a 0-length
sequence.

For https://dev.cfengine.com/issues/7871 the following functions now
take a list or an array or a data container, or inline JSON: filter(),
getindices(), getvalues(), join(), length(), maplist(), reverse(),
unique(), intersection(), difference(), shuffle(), sort(), storejson(),
string_mustache(), sublist(), sum(), product(); all the rewritten
function got new acceptance tests.

Some tests were redundant and thus removed.

The following functions were left alone because they're either not
needed or too tricky due to legacy issues: regarray(), reglist(),
makerule(), nth()

We only create copies of the variables if needed, and free them only in
that case, which will improve performance.

Changelog: Allow inline JSON to be used in function calls (Redmine#7871).
  • Loading branch information
tzz committed Feb 16, 2016
1 parent c92d5b6 commit 18423a4
Show file tree
Hide file tree
Showing 41 changed files with 2,327 additions and 1,505 deletions.
992 changes: 466 additions & 526 deletions libpromises/evalfunction.c

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions libutils/json.c
Expand Up @@ -368,6 +368,14 @@ void JsonDestroy(JsonElement *element)
}
}

void JsonDestroyMaybe(JsonElement *element, bool allocated)
{
if (allocated)
{
JsonDestroy(element);
}
}

JsonElement *JsonArrayMergeArray(const JsonElement *a, const JsonElement *b)
{
assert(JsonGetElementType(a) == JsonGetElementType(b));
Expand Down
7 changes: 7 additions & 0 deletions libutils/json.h
Expand Up @@ -149,6 +149,13 @@ JsonElement *JsonMerge(const JsonElement *a, const JsonElement *b);
*/
void JsonDestroy(JsonElement *element);

/**
@brief Destroy a JSON element if needed
@param element [in] The JSON element to destroy.
@param allocated [in] Whether the element was allocated and needs to be destroyed.
*/
void JsonDestroyMaybe(JsonElement *element, bool allocated);

/**
@brief Get the length of a JsonElement. This is the number of elements or fields in an array or object respectively.
@param element [in] The JSON element.
Expand Down
6 changes: 5 additions & 1 deletion libutils/sequence.c
Expand Up @@ -312,11 +312,15 @@ size_t SeqLength(const Seq *seq)

void SeqShuffle(Seq *seq, unsigned int seed)
{
if (0 == SeqLength(seq))
{
return;
}

/* Store current random number state for being reset at the end of function */
int rand_state = rand();

srand(seed);

for (size_t i = SeqLength(seq) - 1; i > 0; i--)
{
size_t j = rand() % (i + 1);
Expand Down
143 changes: 0 additions & 143 deletions tests/acceptance/01_vars/02_functions/difference_intersection.cf

This file was deleted.

0 comments on commit 18423a4

Please sign in to comment.