mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-08 06:45:38 +00:00
fix(sort): avoid int64 overflow comparing large integers
The int/int comparator returned int(lhs - rhs); the int64 subtraction overflows for large-magnitude values, giving the wrong sign. Compare directly instead.
This commit is contained in:
parent
e2f1d5ccf7
commit
ecc87a4081
@ -166,7 +166,12 @@ func (a sortableNodeArray) compare(lhs *CandidateNode, rhs *CandidateNode, dateT
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
return int(lhsNum - rhsNum)
|
if lhsNum < rhsNum {
|
||||||
|
return -1
|
||||||
|
} else if lhsNum > rhsNum {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
|
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
|
||||||
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -171,6 +171,15 @@ var sortByOperatorScenarios = []expressionScenario{
|
|||||||
"D0, P[], (!!seq)::# abc\n- def\n# ghi\n",
|
"D0, P[], (!!seq)::# abc\n- def\n# ghi\n",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
skipDoc: true,
|
||||||
|
description: "Sort large integers (no int64 subtraction overflow)",
|
||||||
|
document: "[5000000000000000000, -5000000000000000000]",
|
||||||
|
expression: `sort`,
|
||||||
|
expected: []string{
|
||||||
|
"D0, P[], (!!seq)::[-5000000000000000000, 5000000000000000000]\n",
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSortByOperatorScenarios(t *testing.T) {
|
func TestSortByOperatorScenarios(t *testing.T) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user