Skip to content

Commit

Permalink
internal/core/adt: fix pointer comparison issue in equalTerminal
Browse files Browse the repository at this point in the history
This fixes a good ole do not pass a nil pointer of a concrete
value to an interface argument bug.

Fixes #3194

Signed-off-by: Marcel van Lohuizen <mpvl@gmail.com>
Change-Id: Ib16cb8667fe56754b03a9dc3c75fdc363c9abd2b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196094
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
mpvl committed Jun 10, 2024
1 parent 2785aa4 commit 1376fa8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cue/testdata/definitions/hidden.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ diff old new
- ./in.cue:6:9
- ./in.cue:13:13
./in.cue:16:9

Result:
@@ -13,9 +11,6 @@
}
Expand Down
93 changes: 82 additions & 11 deletions cue/testdata/eval/v0.7.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,26 @@ mutual: t4: ok: {
check: p1 & p2 & p3 & p4 & p5
}

-- equality.cue --
issue3194: reduced: {
A: *0 | >10
A: *0 | >10
{
out: close({ b: A })
out: b: A
}.out
}

-- out/eval/stats --
Leaks: 0
Freed: 261
Reused: 249
Allocs: 12
Retain: 166
Leaks: 4
Freed: 285
Reused: 273
Allocs: 16
Retain: 170

Unifications: 261
Conjuncts: 1578
Disjuncts: 408
Unifications: 269
Conjuncts: 1619
Disjuncts: 436
-- out/eval --
Errors:
definition.t0.err.d1.env.c: field not allowed:
Expand Down Expand Up @@ -377,6 +387,12 @@ Result:
}
}
}
issue3194: (struct){
reduced: (#struct){
A: (number){ |(*(int){ 0 }, (number){ >10 }) }
b: (number){ |(*(int){ 0 }, (number){ >10 }) }
}
}
expr: (struct){
t1: (struct){
ok: (struct){
Expand Down Expand Up @@ -666,6 +682,16 @@ Result:
}
}
}
-- out/evalalpha/stats --
Leaks: 288
Freed: 16
Reused: 16
Allocs: 288
Retain: 0

Unifications: 258
Conjuncts: 2303
Disjuncts: 44
-- out/evalalpha --
Errors:
definition.t0.err.d1.env.c: field not allowed:
Expand Down Expand Up @@ -812,6 +838,12 @@ Result:
}
}
}
issue3194: (struct){
reduced: (#struct){
A: (number){ |(*(int){ 0 }, (number){ >10 }) }
b: (number){ |(*(int){ 0 }, (number){ >10 }) }
}
}
expr: (struct){
t1: (struct){
ok: (struct){
Expand Down Expand Up @@ -1096,6 +1128,28 @@ Result:
}
}
}
-- diff/-out/evalalpha/stats<==>+out/eval/stats --
diff old new
--- old
+++ new
@@ -1,9 +1,9 @@
-Leaks: 4
-Freed: 285
-Reused: 273
-Allocs: 16
-Retain: 170
+Leaks: 288
+Freed: 16
+Reused: 16
+Allocs: 288
+Retain: 0

-Unifications: 269
-Conjuncts: 1619
-Disjuncts: 436
+Unifications: 258
+Conjuncts: 2303
+Disjuncts: 44
-- diff/-out/evalalpha<==>+out/eval --
diff old new
--- old
Expand Down Expand Up @@ -1210,7 +1264,7 @@ diff old new
}
#A: (#struct){
a: (int){ int }
@@ -196,8 +179,7 @@
@@ -202,8 +185,7 @@
}
}
}
Expand All @@ -1220,7 +1274,7 @@ diff old new
t0: (struct){
ok: (struct){
c: (struct){
@@ -264,134 +246,130 @@
@@ -270,134 +252,130 @@
}
}
}
Expand Down Expand Up @@ -1479,7 +1533,7 @@ diff old new
}
}
}
@@ -401,23 +379,23 @@
@@ -407,23 +385,23 @@
d: (struct){
b: (int){ 2 }
}
Expand Down Expand Up @@ -1695,6 +1749,23 @@ Reordering / positions.
}
}
}
--- equality.cue
{
issue3194: {
reduced: {
A: (*0|>10)
A: (*0|>10)
{
out: close({
b: 〈2;A〉
})
out: {
b: 〈2;A〉
}
}.out
}
}
}
--- expr.cue
{
expr: {
Expand Down
21 changes: 13 additions & 8 deletions internal/core/adt/disjunct2.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ func isEqualNodeValue(x, y *nodeContext) bool {
if x.hasTop != y.hasTop {
return false
}
if !isEqualBaseValue(x.ctx, x.scalar, y.scalar) {
if !isEqualValue(x.ctx, x.scalar, y.scalar) {
return false
}

Expand All @@ -718,10 +718,10 @@ func isEqualNodeValue(x, y *nodeContext) bool {
return false
}

if !isEqualBaseValue(x.ctx, x.lowerBound, y.lowerBound) {
if !isEqualValue(x.ctx, x.lowerBound, y.lowerBound) {
return false
}
if !isEqualBaseValue(x.ctx, x.upperBound, y.upperBound) {
if !isEqualValue(x.ctx, x.upperBound, y.upperBound) {
return false
}

Expand Down Expand Up @@ -749,15 +749,20 @@ func isEqualNodeValue(x, y *nodeContext) bool {
return true
}

func isEqualBaseValue(ctx *OpContext, x, y BaseValue) bool {
type ComparableValue interface {
comparable
Value
}

func isEqualValue[P ComparableValue](ctx *OpContext, x, y P) bool {
var zero P

if x == y {
return true
}
xv, _ := x.(Value)
yv, _ := y.(Value)
if xv == nil || yv == nil {
if x == zero || y == zero {
return false
}

return Equal(ctx, xv, yv, CheckStructural)
return Equal(ctx, x, y, CheckStructural)
}
4 changes: 2 additions & 2 deletions internal/core/export/testdata/selfcontained/import.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ diff old new
- y: x
-}
+v: V

// Do not simplify because of multiple usages of enclosing struct.
x: B.c
@@ -24,6 +21,12 @@
run: exec.Run
_hidden: int

+//cue:path: "mod.test/a/pkg".v.v
+let V = {
+ x: 3
Expand Down

0 comments on commit 1376fa8

Please sign in to comment.