diff options
| author | Chris Walz <[email protected]> | 2020-11-08 11:56:26 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-11-08 18:56:26 +0200 |
| commit | 246bd25c47871a148cf3eec2ba4b1ba0ec5809ad (patch) | |
| tree | 68b43f47fe803d8fe21de68169c83385d30842a3 | |
| parent | 5c05e978ea43e9992b04d9542bcab5f0b323a985 (diff) | |
fix crash when COMP_POINT is greater than COMP_LINE size (#128)
Some shells (inexplicably) occasionally have a COMP_POINT that is greater than the COMP_LINE size. When that happens completion should be based on length of the COMP_LINE to avoid an out of bounds error.
See the original hack fix here: https://github.com/chriswalz/bit/blob/master/cmd/bitcomplete.go on line 39
| -rw-r--r-- | complete.go | 3 | ||||
| -rw-r--r-- | complete_test.go | 2 |
2 files changed, 4 insertions, 1 deletions
diff --git a/complete.go b/complete.go index 9ddfee2..2a4c905 100644 --- a/complete.go +++ b/complete.go @@ -78,6 +78,9 @@ func Complete(name string, cmd Completer) { if err != nil { panic("COMP_POINT env should be integer, got: " + point) } + if i > len(line) { + i = len(line) + } // Parse the command line up to the completion point. args := arg.Parse(line[:i]) diff --git a/complete_test.go b/complete_test.go index 04b10cc..3a0809f 100644 --- a/complete_test.go +++ b/complete_test.go @@ -173,7 +173,7 @@ func TestComplete(t *testing.T) { {shouldExit: false, line: "", point: ""}, {shouldPanic: true, line: "cmd", point: ""}, {shouldPanic: true, line: "cmd", point: "a"}, - {shouldPanic: true, line: "cmd", point: "4"}, + {shouldExit: true, line: "cmd", point: "4"}, {shouldExit: true, install: "1"}, {shouldExit: false, install: "a"}, |
