Fix FlowRuleComparator violating the Comparator contract - #3629
Fix FlowRuleComparator violating the Comparator contract#3629vasiliy-mikhailov wants to merge 1 commit into
Conversation
FlowRuleComparator.compare returned 0 (equal) for rules that are not equal: - when one limitApp is null and the other is not (the early o1.getLimitApp() == null check returned 0 regardless of o2), and - when both limitApps are non-null, non-default and different (the final else returned 0). Returning 0 for unequal elements breaks the Comparator contract and can drop rules when they are stored in a sorted structure. Handle the null cases explicitly and compare two different non-default limitApps with String.compareTo.
oss-sentinel-ai
left a comment
There was a problem hiding this comment.
Summary
Fixes two Comparator contract violations in FlowRuleComparator: (1) a null limitApp on one side was compared as equal to anything (breaking antisymmetry), and (2) two different non-default limitApp values compared as 0, making the ordering nondeterministic. Both could trigger IllegalArgumentException: Comparison method violates its general contract! from TimSort during rule reloads. Verified against FlowRuleUtil.buildFlowRuleMap (the only consumer): origin-specific rules still sort before default, cluster-mode rules still sort last, and blank limitApp is normalized to default before sorting — so matching semantics are preserved while the ordering becomes deterministic. Tests cover both the null-vs-non-null and different-app cases with antisymmetry assertions. LGTM.
Automated review by github-manager-bot
| return -1; | ||
| } else { | ||
| return 0; | ||
| return o1.getLimitApp().compareTo(o2.getLimitApp()); |
There was a problem hiding this comment.
Verified the surrounding semantics: FlowRuleUtil.buildFlowRuleMap sorts per-resource rule lists with this comparator. Origin-specific rules still sort before LIMIT_APP_DEFAULT and cluster-mode rules still sort last, so rule matching order is preserved; the new lexicographic tie-break between two different non-default limitApp values only makes the ordering deterministic. Combined with FlowRuleUtil normalizing blank limitApp to default before sorting, this safely fixes the Comparator contract violations (antisymmetry) that could trigger IllegalArgumentException from TimSort on rule updates.
Problem
FlowRuleComparator.comparereturns0(equal) for rules that are not actually equal:So a rule whose
limitAppisnullcompares equal to any other rule, and two rules with different non-defaultlimitAppvalues (e.g.originAvsoriginB) compare equal. Returning0for unequal elements violates theComparatorcontract and can cause rules to be dropped when stored in a sorted structure.Fix
0only when bothlimitApps arenull; otherwise order anullconsistently against a non-null value.limitApps withString.compareToinstead of returning0.Test
Adds
testNullVsNonNullLimitAppandtestDifferentNonDefaultLimitAppstoFlowRuleComparatorTest, each also checking antisymmetry. Both fail on1.8(comparereturns0for unequal rules) and pass with this change.AI assistance disclosure
This contribution was produced with the help of an AI pipeline. The pipeline processed a large amount of source code to surface suspected bugs, reproduced a subset of them with failing unit tests and generated candidate fixes, and prepared pull requests from the ones that held up. Each PR was then reviewed and verified by a human before being opened: the fix and test were checked by hand and the test was confirmed to fail before the change and pass after.