this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

lsp/fscache: correct path components

[OverlayFS.pathComponents] was faulty: it was directly casting a
DocumentURI to a string, and then decomposing the string (splitting on
/). This faulty because DocumentURI is URI encoded, so spaces will
appear as %20, and these would continue to exist in the split strings.

Issue #4255

Signed-off-by: Matthew Sackman <matthew@cue.works>
Change-Id: If77b46d4f0a48b8efdec49b764741cb7091f69ed
Reviewed-on: https://cue.gerrithub.io/c/cue-lang/cue/+/1230705
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>

+16 -6
+15 -5
internal/lsp/fscache/fs_overlay.go
··· 8 8 "io" 9 9 iofs "io/fs" 10 10 "os" 11 + "path/filepath" 11 12 "slices" 12 13 "strings" 13 14 "sync" ··· 220 221 // may be empty), and the final basename. 221 222 func (fs *OverlayFS) pathComponents(uri protocol.DocumentURI) ([]string, string) { 222 223 const fileSchemePrefix = "file:///" 223 - str := string(uri) 224 - if !strings.HasPrefix(str, fileSchemePrefix) { 225 - panic(fmt.Sprintf("%q is not a valid DocumentURI", str)) 224 + if !strings.HasPrefix(string(uri), fileSchemePrefix) { 225 + panic(fmt.Sprintf("%q is not a valid DocumentURI", uri)) 226 + } 227 + str := filepath.Clean(uri.Path()) 228 + if str == "." { // if uri.Path() returns "", filepath.Clean() returns "." 229 + return nil, "" 230 + } 231 + components := strings.Split(str, string(os.PathSeparator)) 232 + if len(components) > 0 && components[0] == "" { 233 + // unix: the path is absolute so components[0] will be empty 234 + components = components[1:] 235 + } 236 + if len(components) == 0 { 237 + return nil, "" 226 238 } 227 - components := strings.Split(str[len(fileSchemePrefix):], "/") 228 - // strings.Split always returns a slice of at least 1 element 229 239 idx := len(components) - 1 230 240 return components[:idx], components[idx] 231 241 }