{"record":{"id":"60e3be38113c88d7","repo":"coreybutler/nvm-windows","slug":"failed-to-encode-path-w","errorCode":null,"errorMessage":"failed to encode path: %w","messagePattern":"failed to encode path: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/upgrade/upgrade.go","lineNumber":1062,"sourceCode":"\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer file.Close()\n\t\t\t_, err = io.Copy(writer, file)\n\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t}\n\n\t\treturn nil\n\t})\n}\n\nfunc setHidden(path string) error {\n\t// Convert the path to a UTF-16 encoded string\n\tlpFileName, err := syscall.UTF16PtrFromString(path)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to encode path: %w\", err)\n\t}\n\n\t// Call the Windows API function\n\tret, _, err := syscall.NewLazyDLL(\"kernel32.dll\").\n\t\tNewProc(\"SetFileAttributesW\").\n\t\tCall(\n\t\t\tuintptr(unsafe.Pointer(lpFileName)),\n\t\t\tuintptr(FILE_ATTRIBUTE_HIDDEN),\n\t\t)\n\n\t// Check the result\n\tif ret == 0 {\n\t\treturn fmt.Errorf(\"failed to set hidden attribute: %w\", err)\n\t}\n\treturn nil\n}\n","sourceCodeStart":1044,"sourceCodeEnd":1079,"githubUrl":"https://github.com/coreybutler/nvm-windows/blob/5b18223ca19ff50d707f35410dbc6bd440a9f74d/src/upgrade/upgrade.go#L1044-L1079","documentation":"setHidden marks the .update directory hidden by calling kernel32's SetFileAttributesW via syscall, and first converts the path to a UTF-16 pointer. This error means syscall.UTF16PtrFromString rejected the path — in Go that happens only when the string contains a NUL rune, which renders it unusable as a C wide string. A real path essentially never contains NUL, so this firing indicates a corrupted constructed path rather than a user-visible input problem.","triggerScenarios":"filepath.Join(currentPath, \".update\") somehow containing an embedded NUL (memory corruption or a malformed currentPath from os.Executable); on non-Windows builds where path handling produces unexpected runes; programmatically calling setHidden with attacker- or config-controlled strings containing \\x00.","commonSituations":"Effectively never in normal operation; conceivable with exotic environments that mangle os.Executable output or with NUL bytes injected via misconfigured NVM_HOME.","solutions":["Treat it as a signal of path corruption: log the exact path passed to setHidden and inspect where it was built.","Sanitize NVM_HOME/currentPath: reject strings containing \\x00 before use.","Note the failure is cosmetic — the directory just stays visible; nothing else breaks."],"exampleFix":"// before\nlpFileName, err := syscall.UTF16PtrFromString(path)\nif err != nil {\n    return fmt.Errorf(\"failed to encode path: %w\", err)\n}\n\n// after: validate NUL-free explicitly and surface which path was bad\nif strings.ContainsRune(path, 0) {\n    return fmt.Errorf(\"invalid path (contains NUL): %q\", path)\n}\nlpFileName, err := syscall.UTF16PtrFromString(path)\nif err != nil {\n    return fmt.Errorf(\"failed to encode path %q: %w\", path, err)\n}","handlingStrategy":"type-guard","validationCode":"if strings.ContainsRune(path, 0) {\n    return fmt.Errorf(\"refusing to hide path containing NUL: %q\", path)\n}","typeGuard":"func isValidWin32Path(p string) bool {\n    return p != \"\" && !strings.ContainsRune(p, 0)\n}","tryCatchPattern":"Treat as best-effort: the hidden attribute is cosmetic, so on UTF16PtrFromString failure log and continue rather than failing the upgrade.","preventionTips":["Validate NVM_HOME contains no control characters at install time.","Never construct paths from unsanitized config strings.","Treat NUL-in-path as corruption and investigate upstream path building."],"tags":["windows-api","syscall","paths","encoding"],"backgroundTag":null,"analyzedSha":"5b18223ca19ff50d707f35410dbc6bd440a9f74d","analyzedAt":"2026-08-15T10:06:51.428Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}