elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Panic when setting edit:-dot inappropriately

Open hanche opened this issue 3 years ago • 2 comments

The transcript speaks for itself. At present, edit:-dot is a footgun. It could use a safety.

Suggestion: Set it to the go equivalent of (math:max 0 (math:min $edit:-dot (count $edit:current-command))).

⬥ set edit:-dot = 1
⬥                                                                                                     ~
goroutine 1 [running]:
github.com/elves/elvish/pkg/sys.DumpStack(0xc0001bd100, 0x1483c60)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/sys/dumpstack.go:10 +0x9f
github.com/elves/elvish/pkg/shell.handlePanic()
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/shell/interact.go:39 +0x85
panic(0x1483c60, 0xc0003c94a0)
	/opt/local/lib/go/src/runtime/panic.go:969 +0x1b9
github.com/elves/elvish/pkg/cli.(*CodeBuffer).InsertAtDot(...)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/codearea.go:90
github.com/elves/elvish/pkg/cli.(*codeArea).handleKeyEvent(0xc00007a100, 0x61, 0x14d2300)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/codearea.go:325 +0x739
github.com/elves/elvish/pkg/cli.(*codeArea).Handle(0xc00007a100, 0x152ff20, 0xc0002eeb28, 0x0)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/codearea.go:164 +0xb6
github.com/elves/elvish/pkg/cli.(*app).handle(0xc000268000, 0x1460fc0, 0xc0002eeb28)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/app.go:171 +0x27f
github.com/elves/elvish/pkg/cli.(*loop).Run(0xc00006b280, 0xc00021e101, 0xc000334780, 0xc000268000, 0xc0002ee7b0)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/loop.go:129 +0x242
github.com/elves/elvish/pkg/cli.(*app).ReadCode(0xc000268000, 0x0, 0x0, 0x0, 0x0)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/app.go:334 +0x3ed
github.com/elves/elvish/pkg/edit.(*Editor).ReadCode(0xc00006afc0, 0xc0000a2010, 0xc0000a2008, 0xc0000a2010, 0xc0004f8ed8)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/edit/editor.go:99 +0x33
github.com/elves/elvish/pkg/shell.Interact(0xc0000a2000, 0xc0000a2008, 0xc0000a2010, 0xc00018fcc0)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/shell/interact.go:81 +0x3bd
github.com/elves/elvish/pkg/shell.program.Run(0xc0000a2000, 0xc0000a2008, 0xc0000a2010, 0xc0002452d0, 0xc00009c0b0, 0x0, 0x0, 0x17cd000, 0xc00018ff68)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/shell/shell.go:40 +0x2de
github.com/elves/elvish/pkg/prog.Run(0xc0000a2000, 0xc0000a2008, 0xc0000a2010, 0xc00009c090, 0x3, 0x3, 0xc00018ff48, 0x3, 0x3, 0x0)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/prog/prog.go:136 +0x258
main.main()
	/solid/pkg/gopkg/src/github.com/elves/elvish/main.go:17 +0x11f

goroutine 5 [chan receive]:
github.com/elves/elvish/pkg/eval.getBlackholeChan.func1(0xc00021e060)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/eval/port.go:67 +0x47
created by github.com/elves/elvish/pkg/eval.getBlackholeChan
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/eval/port.go:66 +0x58

goroutine 7 [syscall]:
os/signal.signal_recv(0x1534f20)
	/opt/local/lib/go/src/runtime/sigqueue.go:144 +0x9d
os/signal.loop()
	/opt/local/lib/go/src/os/signal/signal_unix.go:23 +0x25
created by os/signal.Notify.func1.1
	/opt/local/lib/go/src/os/signal/signal.go:150 +0x45

goroutine 9 [chan receive]:
github.com/elves/elvish/pkg/shell.setupShell.func1(0xc00006c840, 0xc0000a2000, 0xc0000a2008, 0xc0000a2010)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/shell/shell.go:51 +0xa5
created by github.com/elves/elvish/pkg/shell.setupShell
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/shell/shell.go:50 +0xf9

goroutine 10 [chan receive]:
github.com/elves/elvish/pkg/cli/prompt.(*Prompt).loop(0xc000245f10)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/prompt/prompt.go:77 +0xc8
created by github.com/elves/elvish/pkg/cli/prompt.New
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/prompt/prompt.go:70 +0x165

goroutine 11 [chan receive]:
github.com/elves/elvish/pkg/cli/prompt.(*Prompt).loop(0xc000266000)
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/prompt/prompt.go:77 +0xc8
created by github.com/elves/elvish/pkg/cli/prompt.New
	/solid/pkg/gopkg/src/github.com/elves/elvish/pkg/cli/prompt/prompt.go:70 +0x165

runtime error: slice bounds out of range [:1] with length 0

hanche avatar Feb 07 '21 16:02 hanche

I forgot to mention that the panic does not occur until you try to type something at the prompt.

hanche avatar Feb 07 '21 16:02 hanche

I was contemplating fixing this along the lines proposed by @hanche and wondered should it be fixed in that fashion? The variable is currently marked experimental (by virtue of the - prefix). It's unclear, @xiaq, why it is marked experimental. I'm guessing it has to do with the dichotomy between byte and rune indexing and how to resolve the ambiguity. For example, what should happen if the var is set to something other than the offset of the first byte of a multi-byte UTF-8 sequence? The implementation could modify the value to be the start of the UTF-8 sequence. But that would be papering over a bug in the Elvish script that is modifying edit:-dot.

I can't help but think that most of the user visible Elvish API should work in terms of runes. With each byte of an invalid UTF-8 sequence treated as a rune when it comes to indexing into a string.

krader1961 avatar May 28 '21 04:05 krader1961