Skip to content

Fix off-by-one error in validation within GetOffsetAtPosition #1669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 24, 2022

Conversation

andyleejordan
Copy link
Member

The incoming line number is 1-indexed (not 0-indexed), so while the
lower limit was correctly set to 1, the upper limit was set to
FileLines.Count which could be 0, but 0 lines implies an upper limit
of 1 with this indexing. See ScriptFile.GetLine for the only other use
of this 1-index range validation.

Fixes #1663.

The incoming line number is 1-indexed (not 0-indexed), so while the
lower limit was correctly set to 1, the upper limit was set to
`FileLines.Count` which could be 0, but 0 lines implies an upper limit
of 1 with this indexing. See `ScriptFile.GetLine` for the only other use
of this 1-index range validation.
@@ -411,7 +411,7 @@ public void ApplyChange(FileChange fileChange)
/// <returns>The zero-based offset for the given file position.</returns>
public int GetOffsetAtPosition(int lineNumber, int columnNumber)
{
Validate.IsWithinRange("lineNumber", lineNumber, 1, this.FileLines.Count);
Validate.IsWithinRange("lineNumber", lineNumber, 1, this.FileLines.Count + 1);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this should just be a separate check for FileLines.Count == 0? I feel like the check is correct here, it just doesn't account for empty? Maybe it should be Math.Max(this.FileLines.Count, 1)?

Copy link
Member Author

Choose a reason for hiding this comment

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

The use of IsWithinRange does not work well with 1-indexed lists that could be empty. I agree this looks weird, but it's what's done here too:

public string GetLine(int lineNumber)
{
Validate.IsWithinRange(
"lineNumber", lineNumber,
1, this.FileLines.Count + 1);
return this.FileLines[lineNumber - 1];
}

@andyleejordan
Copy link
Member Author

@SeeminglyScience I'm going to merge this for now so that the user reported issue can be tested. We can revert if needed.

@andyleejordan andyleejordan merged commit 96a7e9e into master Jan 24, 2022
@andyleejordan andyleejordan deleted the andschwa/fix-getoffset branch January 24, 2022 20:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Failed to load PowerShellEditorServices with lspconfig (nvim)
2 participants