#756 Escape the paths used for the --include parameter for gometalinter, which uses RE2

This commit is contained in:
w0rp 2017-07-11 23:47:13 +01:00
parent d12e990f73
commit 340c0bbac5
4 changed files with 14 additions and 4 deletions

View File

@ -14,7 +14,7 @@ function! ale_linters#go#gometalinter#GetCommand(buffer) abort
let l:options = ale#Var(a:buffer, 'go_gometalinter_options') let l:options = ale#Var(a:buffer, 'go_gometalinter_options')
return ale#Escape(l:executable) return ale#Escape(l:executable)
\ . ' --include=' . ale#Escape(l:filename) \ . ' --include=' . ale#Escape(ale#util#EscapePCRE(l:filename))
\ . (!empty(l:options) ? ' ' . l:options : '') \ . (!empty(l:options) ? ' ' . l:options : '')
\ . ' ' . ale#Escape(fnamemodify(l:filename, ':h')) \ . ' ' . ale#Escape(fnamemodify(l:filename, ':h'))
endfunction endfunction

View File

@ -161,3 +161,9 @@ function! ale#util#FunctionArgCount(function) abort
return l:count return l:count
endfunction endfunction
" Escape a string so the characters in it will be safe for use inside of PCRE
" or RE2 regular expressions without characters having special meanings.
function! ale#util#EscapePCRE(unsafe_string) abort
return substitute(a:unsafe_string, '\([\-\[\]{}()*+?.^$|]\)', '\\\1', 'g')
endfunction

View File

@ -22,7 +22,7 @@ Execute(The gometalinter callback should return the right defaults):
\ ale_linters#go#gometalinter#GetExecutable(bufnr('')) \ ale_linters#go#gometalinter#GetExecutable(bufnr(''))
AssertEqual AssertEqual
\ ale#Escape('gometalinter') \ ale#Escape('gometalinter')
\ . ' --include=' . ale#Escape(expand('%')) \ . ' --include=' . ale#Escape(ale#util#EscapePCRE(expand('%')))
\ . ' ' . ale#Escape(getcwd()), \ . ' ' . ale#Escape(getcwd()),
\ ale_linters#go#gometalinter#GetCommand(bufnr('')) \ ale_linters#go#gometalinter#GetCommand(bufnr(''))
@ -34,7 +34,7 @@ Execute(The gometalinter callback should use a configured executable):
\ ale_linters#go#gometalinter#GetExecutable(bufnr('')) \ ale_linters#go#gometalinter#GetExecutable(bufnr(''))
AssertEqual AssertEqual
\ ale#Escape('something else') \ ale#Escape('something else')
\ . ' --include=' . ale#Escape(expand('%')) \ . ' --include=' . ale#Escape(ale#util#EscapePCRE(expand('%')))
\ . ' ' . ale#Escape(getcwd()), \ . ' ' . ale#Escape(getcwd()),
\ ale_linters#go#gometalinter#GetCommand(bufnr('')) \ ale_linters#go#gometalinter#GetCommand(bufnr(''))
@ -43,7 +43,7 @@ Execute(The gometalinter callback should use configured options):
AssertEqual AssertEqual
\ ale#Escape('gometalinter') \ ale#Escape('gometalinter')
\ . ' --include=' . ale#Escape(expand('%')) \ . ' --include=' . ale#Escape(ale#util#EscapePCRE(expand('%')))
\ . ' --foobar' \ . ' --foobar'
\ . ' ' . ale#Escape(getcwd()), \ . ' ' . ale#Escape(getcwd()),
\ ale_linters#go#gometalinter#GetCommand(bufnr('')) \ ale_linters#go#gometalinter#GetCommand(bufnr(''))

View File

@ -0,0 +1,4 @@
Execute(ale#util#EscapePCRE should escape strings for PCRE or RE2 appropriately):
AssertEqual '\\\^\$\*\+\?\.\(\)\|\{\}\[\]', ale#util#EscapePCRE('\^$*+?.()|{}[]')
AssertEqual 'abcABC09', ale#util#EscapePCRE('abcABC09')
AssertEqual '/', ale#util#EscapePCRE('/')