vrothberg/vgrep

bsd grep: panic when trying to split a matched string

mojozinc opened this issue · 3 comments

for a specific string
./pack/plugins/start/vim-go/ftplugin/gomod.vim:13: \ | exe 'au! vim-go-gomod-buffer * <buffer>'"
the splitting here doesn't work

vgrep/vgrep.go

Lines 251 to 257 in 9cc3683

spl := bytes.SplitN([]byte(match), []byte{0}, 3)
if gitgrep && !ripgrep {
return string(spl[0]), string(spl[1]), string(spl[2])
}
// the 2nd separator of grep is ":"
splline := bytes.SplitN(spl[1], []byte(":"), 2)
return string(spl[0]), string(splline[0]), string(splline[1])

it fails and causes an index out of range error at line 256, because it didn't split into 3 parts
but only 1, lenght of spl is just 1
I added some print statements and this is the output

╰─➤  go run ~/go/src/github.com/vrothberg/vgrep/vgrep.go vim-go                                                                                    1 ↵
----------------Debugging------------------
>>> before split:
./pack/plugins/start/vim-go/ftplugin/gomod.vim:13:      \ | exe 'au! vim-go-gomod-buffer * <buffer>'"
>>> after splitting:
0 | ./pack/plugins/start/vim-go/ftplugin/gomod.vim:13:      \ | exe 'au! vim-go-gomod-buffer * <buffer>'"
----------------Debugging------------------
panic: runtime error: index out of range [1] with length 1

goroutine 1 [running]:
main.(*vgrep).splitMatch(0xc00005a240, 0xc000126000, 0x76, 0xc000140000, 0x11bce25, 0x32, 0xc000142000, 0x2e5, 0x2e6, 0x0)
        /Users/sjangra/go/src/github.com/vrothberg/vgrep/vgrep.go:264 +0x7ca
main.(*vgrep).grep(0xc00005a240, 0xc00004e540, 0x1, 0x1)
        /Users/sjangra/go/src/github.com/vrothberg/vgrep/vgrep.go:233 +0x308
main.main()
        /Users/sjangra/go/src/github.com/vrothberg/vgrep/vgrep.go:105 +0x46e
exit status 2

When it works successfully, debug output looks like this, for comparison

----------------Debugging------------------
>>> before split:
lruCache.go145func (cache *LRUCache) write(key string, data interface{}) (qdata, error) {
>>> after splitting:
0 | lruCache.go
1 | 145
2 | func (cache *LRUCache) write(key string, data interface{}) (qdata, error) {
----------------Debugging------------------
Index File        Line Content
    0 lru.go         9 cache := new(LRUCache)
    1 lruCache.go   23 type LRUCache struct {
    2 lruCache.go  107 func (cache *LRUCache) init(capacity int) {
    3 lruCache.go  112 func (cache *LRUCache) read(key string) (interface{}, error) {
    4 lruCache.go  125 func (cache *LRUCache) touch(key string) error {
    5 lruCache.go  145 func (cache *LRUCache) write(key string, data interface{}) (qdata, error) {

I realise the problem is because of the different seperator
when it fails the directory is not under git.
and it is running a BSD grep

 grep --version                                                                                                                                1 ↵
grep (BSD grep) 2.5.1-FreeBSD

Thanks for opening the issue and providing all the details, @mojozinc!

It seems that the BSD grep has a different format. I assume that the file|line|content are all split by null bytes as done in gitgrep and ripgrep?

Are you interested in opening a PR? I run Linux only but am more than happy to get vgrep run on more systems.

@vrothberg thank you for replying
yes, I would like to take this up.
I will try to raise PR asap.