Skip to content

Fix crash where lines appended to end of script file causes out of bounds exception #730

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 6 commits into from
Aug 28, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/PowerShellEditorServices/Workspace/ScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,17 @@ public void ValidatePosition(int line, int column)

/// <summary>
/// Throws ArgumentOutOfRangeException if the given position is outside
/// of the file's buffer extents.
/// of the file's buffer extents. If the position is for an insertion (an applied change)
/// the index may be 1 past the end of the file, which is just appended.
/// </summary>
/// <param name="line">The 1-based line to be validated.</param>
/// <param name="column">The 1-based column to be validated.</param>
/// <param name="isInsertion">If true, the position to validate is for an applied change.</param>
public void ValidatePosition(int line, int column, bool isInsertion)
{
// If new content is being added, VSCode sometimes likes to add it a (FileLines.Count + 1),
Copy link
Contributor

@rkeithhill rkeithhill Aug 25, 2018

Choose a reason for hiding this comment

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

Minor nit, should this be "add it at …"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are these comments ok @rkeithhill? Want to make sure I've put them where you want and saying enough, etc.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, they LGTM.

// which used to crash EditorServices. Now we append it on to the end of the file.
// See https://github.com/PowerShell/vscode-powershell/issues/1283
int maxLine = isInsertion ? this.FileLines.Count + 1 : this.FileLines.Count;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a code comment above this to make sure someone else doesn't think they see an off-by-one error with the this.FileLines.Count + 1 and attempt to fix it. :-)

if (line < 1 || line > maxLine)
{
Expand Down Expand Up @@ -372,7 +376,9 @@ public void ApplyChange(FileChange fileChange)
this.ValidatePosition(fileChange.Line, fileChange.Offset, isInsertion: true);
this.ValidatePosition(fileChange.EndLine, fileChange.EndOffset, isInsertion: true);

// If the change is a pure append to the file, we just need to add the new lines on the end
// VSCode sometimes likes to give the change start line as (FileLines.Count + 1).
// This used to crash EditorServices, but we now treat it as an append.
// See https://github.com/PowerShell/vscode-powershell/issues/1283
if (fileChange.Line == this.FileLines.Count + 1)
{
foreach (string addedLine in changeLines)
Copy link
Member

Choose a reason for hiding this comment

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

Would there be a scenario where there's a changed line AND an added line?

Copy link
Member

Choose a reason for hiding this comment

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

If so, the foreach might also need to handle the "between" logic as well.

Copy link
Member

Choose a reason for hiding this comment

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

I guess a better question is... are the changed lines always consecutive

Expand Down