From 7308f9d5a4da8ea4e2a9a0748ceb0921333c11f4 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Wed, 11 Sep 2024 16:04:43 +0200 Subject: prog: allow deeper nesting of conditional fields patching There is a totally valid situation when we could be recursively patching conditional fields: if by changing a field's value we insert new resource constructor calls. It's a bug to skip conditional field patching for them. Allow up to 2 nested patchConditionalFields() calls and panic if there happen to be more. Add a test that reproduces the situation described above. --- prog/expr.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'prog/expr.go') diff --git a/prog/expr.go b/prog/expr.go index cfe6eb70b..e9446ab2c 100644 --- a/prog/expr.go +++ b/prog/expr.go @@ -67,11 +67,14 @@ func makeArgFinder(t *Target, c *Call, unionArg *UnionArg, parents parentStack) } func (r *randGen) patchConditionalFields(c *Call, s *state) (extra []*Call, changed bool) { - if r.inPatchConditional { - return nil, false - } - r.inPatchConditional = true - defer func() { r.inPatchConditional = false }() + if r.patchConditionalDepth > 1 { + // Some nested patchConditionalFields() calls are fine as we could trigger a resource + // constructor via generateArg(). But since nested createResource() calls are prohibited, + // patchConditionalFields() should never be nested more than 2 times. + panic("third nested patchConditionalFields call") + } + r.patchConditionalDepth++ + defer func() { r.patchConditionalDepth-- }() var extraCalls []*Call var anyPatched bool -- cgit mrf-deployment