Skip to content

Conversation

smowton
Copy link
Contributor

@smowton smowton commented Sep 12, 2025

Drive-by fix noticed while looking through alleged bad autofixes (autofix used a < test and CodeQL claimed it was an invalid fix)

@smowton smowton requested a review from a team as a code owner September 12, 2025 10:47
@Copilot Copilot AI review requested due to automatic review settings September 12, 2025 10:47
@github-actions github-actions bot added the JS label Sep 12, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR enhances the JavaScript off-by-one array access detection by recognizing that a less-than test (<) provides the same protection as a not-equal test (!=) when mitigating off-by-one errors in array bounds checking.

  • Added a new getLengthLTGuard function to detect index < array.length conditions
  • Updated the main query logic to exclude cases where either a not-equal or less-than guard exists
  • Added test case demonstrating the fix with i < a.length guard

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
javascript/ql/src/LanguageFeatures/LengthComparisonOffByOne.ql Added getLengthLTGuard function and updated query to consider less-than guards
javascript/ql/test/query-tests/LanguageFeatures/LengthComparisonOffByOne/tst.js Added test case for function with less-than guard to verify the fix

@@ -33,6 +33,18 @@ ConditionGuardNode getLengthLEGuard(Variable index, Variable array) {
)
}

/**
* Gets a condition that checks that `index` is less than or equal to `array.length`.
Copy link
Preview

Copilot AI Sep 12, 2025

Choose a reason for hiding this comment

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

The comment is incorrect. The function getLengthLTGuard checks for index < array.length (strictly less than), not less than or equal to. The comment should say 'Gets a condition that checks that index is less than array.length'.

Suggested change
* Gets a condition that checks that `index` is less than or equal to `array.length`.
* Gets a condition that checks that `index` is less than `array.length`.

Copilot uses AI. Check for mistakes.

@smowton smowton force-pushed the smowton/fix/length-comparison-off-by-one-fp branch from 8c5afd2 to db5c581 Compare September 12, 2025 13:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant