PSReadLine icon indicating copy to clipboard operation
PSReadLine copied to clipboard

`KillRegion` function raises an unhandled exception on empty region

Open matteocoder opened this issue 1 month ago • 0 comments

Prerequisites

  • [x] Write a descriptive title.
  • [x] Make sure you are able to repro it on the latest released version
  • [x] Search the existing issues, especially the pinned issues.

Exception report

### Environment
PSReadLine: 2.4.5
PowerShell: 7.6.0-preview.5
OS: Microsoft Windows 10.0.26100
BufferWidth: 120
BufferHeight: 9001

Last 200 Keys:

 d l i n e o p Tab Tab Spacebar Spacebar Backspace - Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Tab Shift+Tab Spacebar Tab Tab Tab Enter
 d f d f Backspace Backspace Backspace Backspace s e t - p s r e a d l i n e k e y h a n d l e r Spacebar c Backspace - c h o r t Tab Backspace d Tab Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace Backspace n o t e p a d Spacebar . \ p r o f i l e . p s q Backspace 1 Enter
 . Spacebar p r o Tab Enter
 t h i s Spacebar i d Spacebar a Spacebar d Backspace Backspace Backspace Backspace Backspace s Spacebar a Spacebar d e b a Backspace u g Spacebar t e s t LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl+a LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow LeftArrow Ctrl+b Ctrl+b

### Exception

System.ArgumentOutOfRangeException: Index and length must refer to a location within the string. (Parameter 'length')
   at System.Text.StringBuilder.ToString(Int32 startIndex, Int32 length)
   at Microsoft.PowerShell.PSConsoleReadLine.Kill(Int32 start, Int32 length, Boolean prepend)
   at Microsoft.PowerShell.PSConsoleReadLine.KillRegion(Nullable`1 key, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(PSKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics, CancellationToken cancellationToken, Nullable`1 lastRunStatus)

Screenshot

Image

Environment data

PS Version: 7.6.0-preview.5
PS HostName: ConsoleHost (Windows Terminal)
PSReadLine Version: 2.4.5
PSReadLine EditMode: Windows
OS: 10.0.26100.1 (WinBuild.160101.0800)
BufferWidth: 120
BufferHeight: 9001

Steps to reproduce

  1. Bind both the KillRegion and SetMark functions to a chord each (this is what profile.ps1 does in the example above)
  2. Set a mark with `SetMark anywhere on the line, except on the first column
  3. Move the cursor before the mark
  4. Kill the region of text between the cursor and the mark with KillRegion
  5. Try to kill the region again

Expected behavior

Since the region is empty and thus cannot be killed, the function should return a ding.

Actual behavior

An unhandled exception is raised.

matteocoder avatar Dec 19 '25 19:12 matteocoder