Repository: neovim
Issue: nlua_regex Lua Memory Leak?
Labels: bug,lua

```
nvim -u NORC

:lua require('telescope.builtin').find_files()
```

I built neovim w/ these flags

`rm -rf build && CMAKE_EXTRA_FLAGS="-DCMAKE_C_COMPILER=clang -DCLANG_ASAN_UBSAN=1" make`

### Actual behaviour

```
➜ nvim

=================================================================
==1012202==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 3040 byte(s) in 3 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x113aa72 in try_malloc /home/tj/build/neovim/build/../src/nvim/memory.c:71:15
    #2 0x113ac4c in xmalloc /home/tj/build/neovim/build/../src/nvim/memory.c:105:15
    #3 0x14ecd4b in nfa_regcomp /home/tj/build/neovim/build/../src/nvim/regexp_nfa.c:6655:10
    #4 0x14e5841 in vim_regcomp /home/tj/build/neovim/build/../src/nvim/regexp.c:7220:12
    #5 0xf9b1a3 in nlua_regex /home/tj/build/neovim/build/../src/nvim/lua/executor.c:1241:3
    #6 0x1eeecca in lj_BC_FUNCC (/usr/local/bin/nvim+0x1eeecca)

Direct leak of 2016 byte(s) in 4 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1ef0c6a in lj_vm_ffi_call (/usr/local/bin/nvim+0x1ef0c6a)
    #2 0x1f187a6 in lj_ccall_func /home/tj/build/neovim/.deps/build/src/luajit/src/lj_ccall.c:1161:5

Direct leak of 152 byte(s) in 1 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1e9eb42 in luv_new_timer (/usr/local/bin/nvim+0x1e9eb42)

Direct leak of 136 byte(s) in 1 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1e9dccb in luv_spawn (/usr/local/bin/nvim+0x1e9dccb)

Direct leak of 63 byte(s) in 1 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x113aa72 in try_malloc /home/tj/build/neovim/build/../src/nvim/memory.c:71:15
    #2 0x113ac4c in xmalloc /home/tj/build/neovim/build/../src/nvim/memory.c:105:15
    #3 0x113b201 in xmallocz /home/tj/build/neovim/build/../src/nvim/memory.c:182:15
    #4 0x113b36e in xmemdupz /home/tj/build/neovim/build/../src/nvim/memory.c:200:17
    #5 0x179a3ac in shada_read_next_item /home/tj/build/neovim/build/../src/nvim/shada.c:3783:9
    #6 0x1771526 in shada_read_when_writing /home/tj/build/neovim/build/../src/nvim/shada.c:2141:22
    #7 0x173c41a in shada_write /home/tj/build/neovim/build/../src/nvim/shada.c:2897:39
    #8 0x17309d5 in shada_write_file /home/tj/build/neovim/build/../src/nvim/shada.c:3156:35
    #9 0xfc39e8 in getout /home/tj/build/neovim/build/../src/nvim/main.c:656:5
    #10 0xcaf147 in ex_quit /home/tj/build/neovim/build/../src/nvim/ex_docmd.c:6303:7
    #11 0xc34e12 in do_one_cmd /home/tj/build/neovim/build/../src/nvim/ex_docmd.c:1949:5
    #12 0xc1dbff in do_cmdline /home/tj/build/neovim/build/../src/nvim/ex_docmd.c:589:20
    #13 0x1285f8e in nv_colon /home/tj/build/neovim/build/../src/nvim/normal.c:4631:18
    #14 0x126f934 in normal_execute /home/tj/build/neovim/build/../src/nvim/normal.c:1138:3
    #15 0x190867d in state_enter /home/tj/build/neovim/build/../src/nvim/state.c:69:26
    #16 0x122a0d7 in normal_enter /home/tj/build/neovim/build/../src/nvim/normal.c:463:3
    #17 0xfaf83e in main /home/tj/build/neovim/build/../src/nvim/main.c:557:3
    #18 0x7fb7006c90b2 in __libc_start_main /build/glibc-YYA7BZ/glibc-2.31/csu/../csu/libc-start.c:308:16

Indirect leak of 451080 byte(s) in 895 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x2bfd308d  (<unknown module>)

Indirect leak of 293832 byte(s) in 583 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x2bfd2f7e  (<unknown module>)

Indirect leak of 22176 byte(s) in 44 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1ef0c6a in lj_vm_ffi_call (/usr/local/bin/nvim+0x1ef0c6a)
    #2 0x1f187a6 in lj_ccall_func /home/tj/build/neovim/.deps/build/src/luajit/src/lj_ccall.c:1161:5

Indirect leak of 792 byte(s) in 3 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1e9cbf4 in luv_new_pipe (/usr/local/bin/nvim+0x1e9cbf4)

Indirect leak of 160 byte(s) in 5 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x1e87e14 in luv_setup_handle (/usr/local/bin/nvim+0x1e87e14)

Indirect leak of 82 byte(s) in 3 object(s) allocated from:
    #0 0x4d485d in malloc (/usr/local/bin/nvim+0x4d485d)
    #1 0x113aa72 in try_malloc /home/tj/build/neovim/build/../src/nvim/memory.c:71:15
    #2 0x113ac4c in xmalloc /home/tj/build/neovim/build/../src/nvim/memory.c:105:15
    #3 0x113b201 in xmallocz /home/tj/build/neovim/build/../src/nvim/memory.c:182:15
    #4 0x113b36e in xmemdupz /home/tj/build/neovim/build/../src/nvim/memory.c:200:17
    #5 0x113c8fe in xstrdup /home/tj/build/neovim/build/../src/nvim/memory.c:419:10
    #6 0x190a95e in vim_strsave /home/tj/build/neovim/build/../src/nvim/strings.c:58:20
    #7 0x14edfe1 in nfa_regcomp /home/tj/build/neovim/build/../src/nvim/regexp_nfa.c:6686:19
    #8 0x14e5841 in vim_regcomp /home/tj/build/neovim/build/../src/nvim/regexp.c:7220:12
    #9 0xf9b1a3 in nlua_regex /home/tj/build/neovim/build/../src/nvim/lua/executor.c:1241:3
    #10 0x1eeecca in lj_BC_FUNCC (/usr/local/bin/nvim+0x1eeecca)

SUMMARY: AddressSanitizer: 773529 byte(s) leaked in 1543 allocation(s).
```

### Expected behaviour

Either I write better code (which might be the solution?) or maybe it's something here that's causing me to leak memory.

@bfredl