Skip to content

Commit e19ab6f

Browse files
authored
Fix off-by-one on maxlLine calc (#573)
* Fix off-by-one on maxlLine calc The old code allowed the line param to be *equal* to FileLines.Count + 1. That's not a valid index even after subtracting 1 to get to 0-based indices. Also added more data to log messages to help in the future. * Fix bad interpolated string
1 parent c762ff7 commit e19ab6f

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/PowerShellEditorServices/Workspace/ScriptFile.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,22 +279,20 @@ public void ValidatePosition(BufferPosition bufferPosition)
279279
/// <param name="column">The 1-based column to be validated.</param>
280280
public void ValidatePosition(int line, int column)
281281
{
282-
if (line < 1 || line > this.FileLines.Count + 1)
282+
int maxLine = this.FileLines.Count;
283+
if (line < 1 || line > maxLine)
283284
{
284-
throw new ArgumentOutOfRangeException("Position is outside of file line range.");
285+
throw new ArgumentOutOfRangeException($"Position {line}:{column} is outside of the line range of 1 to {maxLine}.");
285286
}
286287

287-
// The maximum column is either one past the length of the string
288+
// The maximum column is either **one past** the length of the string
288289
// or 1 if the string is empty.
289290
string lineString = this.FileLines[line - 1];
290291
int maxColumn = lineString.Length > 0 ? lineString.Length + 1 : 1;
291292

292293
if (column < 1 || column > maxColumn)
293294
{
294-
throw new ArgumentOutOfRangeException(
295-
string.Format(
296-
"Position is outside of column range for line {0}.",
297-
line));
295+
throw new ArgumentOutOfRangeException($"Position {line}:{column} is outside of the column range of 1 to {maxColumn}.");
298296
}
299297
}
300298

0 commit comments

Comments
 (0)