Skip to content

Commit 4638be0

Browse files
committed
internal/core/adt: check for nil state in parent disjunction
Currently, as nodeContext values are never reclaimed, the value will always be non-nil. In certain cases with API usage, though, a node may have a parent Vertex that is finalized and whose state field has already been cleared. We add. a test to expose this. Without the added check, the test would fail with the upcoming memory management implementation. Issue #3334 Signed-off-by: Marcel van Lohuizen <[email protected]> Change-Id: Ibaae384409263fe7aa0ee01a96f49b9000daa387 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1218219 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent ab40e7f commit 4638be0

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

internal/core/adt/api_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 CUE Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package adt_test
16+
17+
import (
18+
"testing"
19+
20+
"cuelang.org/go/cue"
21+
"cuelang.org/go/cue/cuecontext"
22+
)
23+
24+
func closeAll(v cue.Value) cue.Value {
25+
path := cue.MakePath(cue.Def("#x"))
26+
return v.Context().CompileString("#x: _").FillPath(path, v).LookupPath(path)
27+
}
28+
29+
type editConfig struct {
30+
Edits []cue.Value `json:"edits,omitempty"`
31+
}
32+
33+
// Modifying values can cause a mix of non-finalized nodes as children of
34+
// a finalized parent. Ensure that this does not cause issues.
35+
func TestAPIModifyingValues(t *testing.T) {
36+
ctx := cuecontext.New(cuecontext.CUE_DEBUG("logeval=1"))
37+
38+
v := ctx.CompileString(`
39+
edits: [{
40+
type: "replace"
41+
data!: "^wrong"
42+
}]
43+
`)
44+
if err := v.Err(); err != nil {
45+
t.Fatal(err)
46+
}
47+
closed := closeAll(ctx.EncodeType(editConfig{}))
48+
49+
v = v.Unify(closed)
50+
if err := v.Err(); err != nil {
51+
t.Fatal(err)
52+
}
53+
54+
var cfg editConfig
55+
if err := v.Decode(&cfg); err != nil {
56+
t.Fatal(err)
57+
}
58+
59+
w := ctx.CompileString(`
60+
type!: "fill" | "replace"
61+
data!: _
62+
`)
63+
64+
if err := w.Err(); err != nil {
65+
t.Fatal(err)
66+
}
67+
x := cfg.Edits[0]
68+
x = x.Unify(w)
69+
70+
if err := x.Err(); err != nil {
71+
t.Fatal(err)
72+
}
73+
}

internal/core/adt/disjunct2.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,11 @@ func (n *nodeContext) processDisjunctions() *Bottom {
267267
var outerRunMode runMode
268268
for p := n.node; p != nil; p = p.Parent {
269269
if p.IsDisjunct {
270-
outerRunMode = p.state.runMode
270+
if p.state == nil {
271+
outerRunMode = finalize
272+
} else {
273+
outerRunMode = p.state.runMode
274+
}
271275
break
272276
}
273277
}

0 commit comments

Comments
 (0)