-
Notifications
You must be signed in to change notification settings - Fork 234
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
Changes from 1 commit
4eed1bb
08647af
6a17b4a
7bdbc2d
e1cff3d
2bd9a30
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
// 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
if (line < 1 || line > maxLine) | ||
{ | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess a better question is... are the changed lines always consecutive |
||
|
There was a problem hiding this comment.
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 …"?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, they LGTM.