mirror of
https://github.com/mikefarah/yq.git
synced 2026-06-28 07:57:43 +00:00
recurseNodeObjectEqual and containsObject both used findInArray to
locate keys in a MappingNode's Content array. findInArray steps by 1,
so it matches against both keys (even indices) and values (odd indices).
In recurseNodeObjectEqual, when a null key in the LHS matched a null
value in the RHS at the last position, rhs.Content[indexInRHS+1]
accessed an out-of-bounds index, causing a panic.
In containsObject, a %2 guard prevented the panic but introduced false
negatives: when a null value appeared before the actual null key,
findInArray returned the value's odd index, the guard rejected it, and
the function reported the key as missing.
Both functions now use findKeyInMap, which steps by 2 and compares only
key positions. The %2 guard in containsObject is removed.
Reproducer for the panic (recurseNodeObjectEqual):
echo '? [{~: ~}]
: v1
? [{2: ~}]
: v2' | yq '. += .'
Reproducer for the false negative (containsObject):
printf '? 1\n: ~\n? ~\n: x\n' | yq 'contains({~: "x"})'
Found by OSS-Fuzz via the lima project's FuzzEvaluateExpression target.
https://issues.oss-fuzz.com/issues/383860504
Signed-off-by: Jan Dubois <jan@jandubois.com>
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
102 lines
2.4 KiB
Go
102 lines
2.4 KiB
Go
package yqlib
|
|
|
|
import "testing"
|
|
|
|
var containsOperatorScenarios = []expressionScenario{
|
|
{
|
|
skipDoc: true,
|
|
expression: `null | contains(~)`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
skipDoc: true,
|
|
expression: `3 | contains(3)`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
skipDoc: true,
|
|
expression: `3 | contains(32)`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::false\n",
|
|
},
|
|
},
|
|
{
|
|
description: "Array contains array",
|
|
subdescription: "Array is equal or subset of",
|
|
document: `["foobar", "foobaz", "blarp"]`,
|
|
expression: `contains(["baz", "bar"])`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
description: "Array has a subset array",
|
|
subdescription: "Subtract the superset array from the subset, if there's anything left, it's not a subset",
|
|
document: `["foobar", "foobaz", "blarp"]`,
|
|
expression: `["baz", "bar"] - . | length == 0`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::false\n",
|
|
},
|
|
},
|
|
{
|
|
skipDoc: true,
|
|
expression: `["dog", "cat", "giraffe"] | contains(["camel"])`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::false\n",
|
|
},
|
|
},
|
|
{
|
|
description: "Object included in array",
|
|
document: `{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}`,
|
|
expression: `contains({"bar": [{"barp": 12}]})`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
description: "Object not included in array",
|
|
document: `{"foo": 12, "bar":[1,2,{"barp":12, "blip":13}]}`,
|
|
expression: `contains({"foo": 12, "bar": [{"barp": 15}]})`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::false\n",
|
|
},
|
|
},
|
|
{
|
|
// Regression: findInArray could match a null key against a null
|
|
// value at an earlier odd index, producing a false negative.
|
|
skipDoc: true,
|
|
document: "? 1\n: ~\n? ~\n: x",
|
|
expression: `contains({~: "x"})`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
description: "String contains substring",
|
|
document: `"foobar"`,
|
|
expression: `contains("bar")`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
{
|
|
description: "String equals string",
|
|
document: `"meow"`,
|
|
expression: `contains("meow")`,
|
|
expected: []string{
|
|
"D0, P[], (!!bool)::true\n",
|
|
},
|
|
},
|
|
}
|
|
|
|
func TestContainsOperatorScenarios(t *testing.T) {
|
|
for _, tt := range containsOperatorScenarios {
|
|
testScenario(t, &tt)
|
|
}
|
|
documentOperatorScenarios(t, "contains", containsOperatorScenarios)
|
|
}
|