Skip to content

Fix ISXB-1792: HaveDuplicateReferences inner loop bound (n < count no…#2376

Open
K-Tone wants to merge 1 commit intodevelopfrom
fix/ISXB-1792-array-helpers-have-duplicate-references
Open

Fix ISXB-1792: HaveDuplicateReferences inner loop bound (n < count no…#2376
K-Tone wants to merge 1 commit intodevelopfrom
fix/ISXB-1792-array-helpers-have-duplicate-references

Conversation

@K-Tone
Copy link
Collaborator

@K-Tone K-Tone commented Mar 12, 2026

  1. ISXB-1792 – Faulty ArrayHelpers.HaveDuplicateReferences implementation
    Link: https://jira.unity3d.com/browse/ISXB-1792

Why it’s easy: The ticket describes the exact bug and fix. The inner loop uses n < count - i but should use n < count, so the loop doesn’t cover the full range and duplicate detection is wrong.
Fix: In ArrayHelpers.cs (around line 118), change the inner loop condition from n < count - i to n < count.
Extra: Reporter suggests adding a test. Single file, one-line logic fix.

Made-with: Cursor

Description

Please fill this section with a description what the pull request is trying to address and what changes were made.

Testing status & QA

Please describe the testing already done by you and what testing you request/recommend QA to execute. If you used or created any testing project please link them here too for QA.

Overall Product Risks

Please rate the potential complexity and halo effect from low to high for the reviewers. Note down potential risks to specific Editor branches if any.

  • Complexity:
  • Halo Effect:

Comments to reviewers

Please describe any additional information such as what to focus on, or historical info for the reviewers.

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests Area_CanDoX, Area_CanDoX_EvenIfYIsTheCase, Area_WhenIDoX_AndYHappens_ThisIsTheResult.
    • Performance tests.
    • Integration tests.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

…t n < count - i)

- Inner loop was terminating too early, missing duplicate checks for later elements.
- Add Utilities_HaveDuplicateReferences_DetectsDuplicatesInFullRange test including regression for dup at end.

Made-with: Cursor
Copy link
Contributor

@u-pr u-pr bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May require changes
The review identified a logic bug regarding the ignored index parameter, a suggestion for additional test coverage, and a potential issue with value types and ReferenceEquals.

🤖 Helpful? 👍/👎

Comment on lines 120 to +123
for (var i = 0; i < count; ++i)
{
var element = first[i];
for (var n = i + 1; n < count - i; ++n)
for (var n = i + 1; n < count; ++n)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high
While this PR successfully fixes the inner loop's end condition, the method still entirely ignores the index parameter. Both the outer and inner loops iterate up to count but start at 0 instead of index, meaning they will scan the wrong range of elements if index > 0.

Should this be updated to respect the index parameter?

for (var i = index; i < index + count; ++i)
{
    var element = first[i];
    for (var n = i + 1; n < index + count; ++n)
    {
        if (ReferenceEquals(element, first[n]))
            return true;
    }
}

🤖 Helpful? 👍/👎

Comment on lines +126 to +127
withDup[2] = withDup[0]; // duplicate at 0 and 2
Assert.That(withDup.HaveDuplicateReferences(0, 3), Is.True);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium
Have you considered adding a test case with a non-zero starting index (e.g., index: 1)? This would ensure the bounds are correctly respected. Because the implementation of HaveDuplicateReferences currently ignores the index parameter, a test relying on an offset would fail, helping to expose the missing offset bug.

🤖 Helpful? 👍/👎

for (var n = i + 1; n < count - i; ++n)
for (var n = i + 1; n < count; ++n)
{
if (ReferenceEquals(element, first[n]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low
Because ReferenceEquals is used for comparison, this method will always return false for value types (like int or struct) because they are boxed into unique objects when passed as object arguments. Have you considered adding a where TFirst : class constraint to the generic type to prevent accidental misuse with value types?

🤖 Helpful? 👍/👎

@codecov-github-com
Copy link

codecov-github-com bot commented Mar 12, 2026

Codecov Report

All modified and coverable lines are covered by tests ✅

@@           Coverage Diff            @@
##           develop    #2376   +/-   ##
========================================
  Coverage    77.90%   77.91%           
========================================
  Files          476      481    +5     
  Lines        97613    97671   +58     
========================================
+ Hits         76048    76102   +54     
- Misses       21565    21569    +4     
Flag Coverage Δ
inputsystem_MacOS_2022.3 5.50% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_2022.3_project 75.43% <100.00%> (+0.03%) ⬆️
inputsystem_MacOS_6000.0 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.0_project 77.33% <100.00%> (+0.04%) ⬆️
inputsystem_MacOS_6000.3 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.3_project 77.33% <100.00%> (+0.04%) ⬆️
inputsystem_MacOS_6000.4 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.4_project 77.34% <100.00%> (+0.04%) ⬆️
inputsystem_MacOS_6000.5 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.5_project 77.34% <100.00%> (+0.03%) ⬆️
inputsystem_MacOS_6000.6 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_MacOS_6000.6_project 77.34% <100.00%> (+0.04%) ⬆️
inputsystem_Ubuntu_2022.3_project 75.23% <100.00%> (+0.03%) ⬆️
inputsystem_Ubuntu_6000.0 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.0_project 77.13% <100.00%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.3 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.3_project 77.13% <100.00%> (+0.03%) ⬆️
inputsystem_Ubuntu_6000.4 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.4_project 77.15% <100.00%> (+0.04%) ⬆️
inputsystem_Ubuntu_6000.5 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.5_project 77.14% <100.00%> (+0.03%) ⬆️
inputsystem_Ubuntu_6000.6 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Ubuntu_6000.6_project 77.14% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_2022.3 5.50% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_2022.3_project 75.55% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_6000.0 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.0_project 77.46% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_6000.3 5.28% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.3_project 77.46% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_6000.4 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.4_project 77.46% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_6000.5 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.5_project 77.46% <100.00%> (+0.03%) ⬆️
inputsystem_Windows_6000.6 5.29% <0.00%> (-0.03%) ⬇️
inputsystem_Windows_6000.6_project 77.46% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...ts/Tests/InputSystem/Utilities/ArrayHelperTests.cs 100.00% <100.00%> (ø)
....inputsystem/InputSystem/Utilities/ArrayHelpers.cs 79.20% <100.00%> (ø)

... and 6 files with indirect coverage changes

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