Skip to content

Commit 546f525

Browse files
janduboisclaude
andcommitted
Add Kind checks to findInArray and findKeyInMap
findInArray and findKeyInMap accept any *CandidateNode but produce wrong results when called on the wrong Kind: findInArray uses stride 1, correct for SequenceNodes but dangerous on MappingNodes (where key-value pairs live at even-odd indices); findKeyInMap uses stride 2, correct for MappingNodes but silently skips elements in SequenceNodes. Commit b0ba958 fixed two call sites that passed MappingNodes to findInArray, but nothing prevents the same mistake from recurring. Each function now logs a warning and returns -1 on a Kind mismatch, surfacing misuse in tests and debug output rather than letting it corrupt results silently. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3b2423e commit 546f525

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

pkg/yqlib/lib.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ func recurseNodeArrayEqual(lhs *CandidateNode, rhs *CandidateNode) bool {
5252
}
5353

5454
func findInArray(array *CandidateNode, item *CandidateNode) int {
55-
55+
if array.Kind != SequenceNode {
56+
log.Warningf("findInArray called on %v node, expected SequenceNode", array.Tag)
57+
return -1
58+
}
5659
for index := 0; index < len(array.Content); index = index + 1 {
5760
if recursiveNodeEqual(array.Content[index], item) {
5861
return index
@@ -62,7 +65,10 @@ func findInArray(array *CandidateNode, item *CandidateNode) int {
6265
}
6366

6467
func findKeyInMap(dataMap *CandidateNode, item *CandidateNode) int {
65-
68+
if dataMap.Kind != MappingNode {
69+
log.Warningf("findKeyInMap called on %v node, expected MappingNode", dataMap.Tag)
70+
return -1
71+
}
6672
for index := 0; index < len(dataMap.Content); index = index + 2 {
6773
if recursiveNodeEqual(dataMap.Content[index], item) {
6874
return index

0 commit comments

Comments
 (0)