xo / usql

Universal command-line interface for SQL databases
MIT License
8.81k stars 346 forks source link

Crashed after executing the command #464

Open spance opened 1 month ago

spance commented 1 month ago
~$ ./usql_static -V
usql 0.19.1
~$ ./usql_static
Type "help" for help.

panic: runtime error: slice bounds out of range [:43] with capacity 32

goroutine 1 [running]:
github.com/xo/usql/stmt.(*Params).Get(0xc00077c720, 0xc00173ea48)
        github.com/xo/usql/stmt/params.go:86 +0x9fb
github.com/xo/usql/stmt.(*Params).GetAll(0xc00077c720, 0xc00173ea48)
        github.com/xo/usql/stmt/params.go:97 +0x55
github.com/xo/usql/metacmd.(*Params).GetAll(0xc000c320c0, 0x1)
        github.com/xo/usql/metacmd/types.go:202 +0x192
github.com/xo/usql/metacmd.init.0.func15(0xc000c320c0)
        github.com/xo/usql/metacmd/cmds.go:442 +0x178
github.com/xo/usql/handler.(*Handler).Run.Decode.func3({0x6e2e518, 0xc001677200})
        github.com/xo/usql/metacmd/metacmd.go:26 +0xd8
github.com/xo/usql/metacmd.RunnerFunc.Run(0x585b8e0?, {0x6e2e518?, 0xc001677200?})
        github.com/xo/usql/metacmd/types.go:82 +0x4d
github.com/xo/usql/handler.(*Handler).Run(0xc001677200)
        github.com/xo/usql/handler/handler.go:282 +0xb30
main.Run({0x6dd1b40, 0xada1340}, 0xc0010e5a70, 0xc000ca12c0, {0xc000eff020, 0x23})
        github.com/xo/usql/run.go:355 +0xce2
main.New.func3(0xc001407808?, {0xada1340?, 0x7?, 0x6077847?})
        github.com/xo/usql/run.go:126 +0x185
github.com/spf13/cobra.(*Command).execute(0xc001407808, {0xc0000740a0, 0x0, 0x0})
        github.com/spf13/cobra@v1.8.0/command.go:983 +0xaca
github.com/spf13/cobra.(*Command).ExecuteC(0xc001407808)
        github.com/spf13/cobra@v1.8.0/command.go:1115 +0x3ff
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/spf13/cobra.(*Command).ExecuteContext(0xc0000740a0?, {0x6dd1b40?, 0xada1340?})
        github.com/spf13/cobra@v1.8.0/command.go:1032 +0x47
main.main()
        github.com/xo/usql/main.go:40 +0x13c
nineinchnick commented 1 month ago

What's in your $HOME/.usqlrc? What's usql_static - how did you build it?

spance commented 1 month ago

The .usqlrc is an empty file, and the usql_static was downloaded from https://github.com/xo/usql/releases/download/v0.19.1/usql_static-0.19.1-linux-amd64.tar.bz2

nineinchnick commented 1 month ago

Are you sure it's empty? It fails when executing an \echo command, I wonder how it's being triggered and what are the arguments to it. Can you try running it with -X? This disables reading any init files.

spance commented 1 month ago

I tried many times and finally found the problem. My config file $HOME/.config/usql/config.yaml like this:

connections:
  xxx: xxxx
init: |
  \echo date `date`

Then after deleting the init fragment, no errors were reported.

nineinchnick commented 1 month ago

Oh I totally forgot there's a yaml config too, thanks! @kenshaw PTAL, \echo datedate` works with a regular.usqlrc`.

kenshaw commented 1 month ago

I'll look in to this shortly. Probably a simple oversight on my part.

kenshaw commented 1 month ago

I'm not able to reproduce this error:

ken@ken-desktop:~/Downloads$ mkdir usql
ken@ken-desktop:~/Downloads$ cd usql/
[config.yaml.txt](https://github.com/xo/usql/files/15292979/config.yaml.txt)
ken@ken-desktop:~/Downloads/usql$ mv ../usql_static-0.19.1-linux-amd64.tar.bz2 .
ken@ken-desktop:~/Downloads/usql$ tar -jxvf usql_static-0.19.1-linux-amd64.tar.bz2 
usql_static
LICENSE
ken@ken-desktop:~/Downloads/usql$ md5sum usql_static
c110e337bb43dd359c34813bb459366c  usql_static
ken@ken-desktop:~/Downloads/usql$ ./usql_static 
Type "help" for help.

date Mon May 13 05:18:34 PM WIB 2024
(not connected)=> \q
ken@ken-desktop:~/Downloads/usql$ cat ~/.config/usql/config.yaml 
---
init: |
  \echo date `date`
ken@ken-desktop:~/Downloads/usql$ ./usql_static -V
usql 0.19.1
ken@ken-desktop:~/Downloads/usql$ 

Can you please share your exact yaml config file? Note that GitHub doesn't seem to allow uploads of .yaml extensions, for whatever reason. You'll need to rename the file prior to upload.

This is the simple debugging config file that I used: config.txt

kenshaw commented 1 month ago

Are you perhaps using something like Windows encoding? With \r\n line endings? I've tried all manner of variations and I can't cause that same block of code to escape under any input that I could come up with. I'm sure my code is bad, but it's going to be difficult to figure out the cause of this without the exact character sequence/input.

Are you able to get \echo to work in the init: stanza in a simpler form? For example:

init: '\echo hi'

Which has this output on my system:

$ usql
Type "help" for help.

hi
(not connected)=> \q
spance commented 1 month ago

I repeat it again.

~$ cat .config/usql/config.yaml
connections:
  xx: mysql://xxxx
  cr: mysql://xxxx
init: |
  \echo `date`

~$ ./usql
Type "help" for help.

panic: runtime error: slice bounds out of range [:43] with capacity 32

goroutine 1 [running]:
github.com/xo/usql/stmt.(*Params).Get(0xc000d0f660, 0xc00173ea28)
        github.com/xo/usql/stmt/params.go:86 +0x9fb
github.com/xo/usql/metacmd.(*Params).Get(0xc00174ef00, 0x1)
        github.com/xo/usql/metacmd/types.go:164 +0x192
github.com/xo/usql/metacmd.(*Params).GetOptional(0xc0007763c0?, 0xa0?)
        github.com/xo/usql/metacmd/types.go:189 +0x13
github.com/xo/usql/metacmd.init.0.func15(0xc00174ef00)
        github.com/xo/usql/metacmd/cmds.go:428 +0x2c
github.com/xo/usql/handler.(*Handler).Run.Decode.func3({0x6c8a398, 0xc0015fa9c0})
        github.com/xo/usql/metacmd/metacmd.go:26 +0xd8
github.com/xo/usql/metacmd.RunnerFunc.Run(0x56b4360?, {0x6c8a398?, 0xc0015fa9c0?})
        github.com/xo/usql/metacmd/types.go:82 +0x4d
github.com/xo/usql/handler.(*Handler).Run(0xc0015fa9c0)
        github.com/xo/usql/handler/handler.go:282 +0xb30
main.Run({0x6c2d9c0, 0xabad740}, 0xc001277cb0, 0xc000f22bd0, {0xc000f0cd40, 0xd})
        github.com/xo/usql/run.go:355 +0xce2
main.New.func3(0xc00012d808?, {0xabad740?, 0x7?, 0x5ed2453?})
        github.com/xo/usql/run.go:126 +0x185
github.com/spf13/cobra.(*Command).execute(0xc00012d808, {0xc0000740a0, 0x0, 0x0})
        github.com/spf13/cobra@v1.8.0/command.go:983 +0xaca
github.com/spf13/cobra.(*Command).ExecuteC(0xc00012d808)
        github.com/spf13/cobra@v1.8.0/command.go:1115 +0x3ff
github.com/spf13/cobra.(*Command).Execute(...)
        github.com/spf13/cobra@v1.8.0/command.go:1039
github.com/spf13/cobra.(*Command).ExecuteContext(0xc0000740a0?, {0x6c2d9c0?, 0xabad740?})
        github.com/spf13/cobra@v1.8.0/command.go:1032 +0x47
main.main()
        github.com/xo/usql/main.go:40 +0x13c
~$
~$ vim .config/usql/config.yaml
~$ cat .config/usql/config.yaml
connections:
  xx: mysql://xxxx
  cr: mysql://xxxx

~$
~$ ./usql
Type "help" for help.

(not connected)=> \q
~$
spance commented 1 month ago

Now I found the cause of this problem, because the output of the date command contains some unicode characters (non-ascii), causing the variable i calculation in the Params.Get() method to become longer. There is my simple fix:

i = start + len([]rune(z)) - 1   // Line-59

Another solution: utf8.RuneCountInString(z)