fiatjaf / jiq

jid on jq - interactive JSON query tool using jq expressions
MIT License
917 stars 32 forks source link

jiq crashes when typing "sort" in (non-sortable) query expression #21

Closed erhhung closed 4 years ago

erhhung commented 4 years ago

Run the standard sample—or any input—and start typing .|sort As soon as I finish typing the word "sort", jiq crashes:

$ echo '{"aa":"2AA2","bb":{"aaa":[123,"cccc",[1,2]],"c":321}}'| jiq
panic: runtime error: slice bounds out of range [:-1]

goroutine 1 [running]:
github.com/fiatjaf/jiq.(*Engine).getContents(0xc00002a080, 0xc0002920f0, 0xf, 0xf, 0x0, 0x0, 0x0)
    /Users/erhhung/go/src/github.com/fiatjaf/jiq/engine.go:189 +0x357
github.com/fiatjaf/jiq.(*Engine).Run(0xc00002a080, 0x0)
    /Users/erhhung/go/src/github.com/fiatjaf/jiq/engine.go:69 +0x12e
main.run(0xc00002a080, 0xc00000e000, 0xc0000101d0)
    /Users/erhhung/go/src/github.com/fiatjaf/jiq/cmd/jiq/jiq.go:53 +0x32
main.main()
    /Users/erhhung/go/src/github.com/fiatjaf/jiq/cmd/jiq/jiq.go:49 +0x14d

macOS 10.15.6, go1.15, jiq 0.7.0, jq-1.6

erhhung commented 4 years ago

This is really a jq error handling issue:

$ echo '{"aa":"2AA2","bb":{"aaa":[123,"cccc",[1,2]],"c":321}}'| jq '.|sort'
jq: error (at <stdin>:1): object ({"aa":"2AA2...) cannot be sorted, as it is not an array
fiatjaf commented 4 years ago

jiq should be catching these errors though. It does for everything else.

erhhung commented 4 years ago

I just "git pull"ed your master branch half hour ago to verify the "error+output" enhancement, but hit this issue.