Skip to content

Conversation

@cannist
Copy link
Owner

@cannist cannist commented Mar 4, 2025

No description provided.

@@ -0,0 +1,3 @@
function endsWith(x, y) {
return x.lastIndexOf(y) === x.length - y.length;

Check failure

Code scanning / CodeQL

Incorrect suffix check High

This suffix check is missing a length comparison to correctly handle lastIndexOf returning -1.

Copilot Autofix

AI 10 months ago

To fix the problem, we need to ensure that the lastIndexOf method does not return -1 before comparing it to x.length - y.length. This can be achieved by adding an explicit check for the -1 case. If lastIndexOf returns -1, it means y is not a substring of x, and we should return false. Otherwise, we proceed with the original comparison.

Suggested changeset 1
my.js

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/my.js b/my.js
--- a/my.js
+++ b/my.js
@@ -1,3 +1,4 @@
 function endsWith(x, y) {
-  return x.lastIndexOf(y) === x.length - y.length;
+  let index = x.lastIndexOf(y);
+  return index !== -1 && index === x.length - y.length;
 }
EOF
@@ -1,3 +1,4 @@
function endsWith(x, y) {
return x.lastIndexOf(y) === x.length - y.length;
let index = x.lastIndexOf(y);
return index !== -1 && index === x.length - y.length;
}
Copilot is powered by AI and may make mistakes. Always verify output.
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.

2 participants