Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Merged
andyleejordan merged 1 commit into
masterPowerShell/PowerShellEditorServices:masterfrom
andschwa/fix-getoffsetPowerShell/PowerShellEditorServices:andschwa/fix-getoffsetCopy head branch name to clipboard
Jan 24, 2022
Merged

Fix off-by-one error in validation within GetOffsetAtPosition#1669
andyleejordan merged 1 commit into
masterPowerShell/PowerShellEditorServices:masterfrom
andschwa/fix-getoffsetPowerShell/PowerShellEditorServices:andschwa/fix-getoffsetCopy head branch name to clipboard

Conversation

@andyleejordan

Copy link
Copy Markdown
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.
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.