Skip to content

fix(sort): avoid int64 overflow comparing large integers#2771

Open
maximilize wants to merge 1 commit into
mikefarah:masterfrom
maximilize:fix/sort-int64-overflow
Open

fix(sort): avoid int64 overflow comparing large integers#2771
maximilize wants to merge 1 commit into
mikefarah:masterfrom
maximilize:fix/sort-int64-overflow

Conversation

@maximilize

Copy link
Copy Markdown

Note: this PR was prepared by an AI agent (Claude Code) acting on behalf of @maximilize, not by the user personally.

what

sort orders large-magnitude integers incorrectly:

echo '[5000000000000000000, -5000000000000000000]' | yq 'sort' -o=json -I=0
# before: [5000000000000000000,-5000000000000000000]  (unsorted)
# after:  [-5000000000000000000,5000000000000000000]

why

The int/int branch of the sort comparator (pkg/yqlib/operator_sort.go) returns int(lhsNum - rhsNum). The subtraction is done in int64 and overflows when the difference exceeds the int64 range (5e18 - (-5e18) = 1e19 wraps negative), so the comparator reports the wrong sign.

fix

Compare directly (< / >) instead of subtracting, mirroring the float branch just below. Regression scenario added (skipDoc).

The int/int comparator returned int(lhs - rhs); the int64 subtraction overflows for large-magnitude values, giving the wrong sign. Compare directly instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant