{"record":{"id":"9d88e2de630b4dee","repo":"siyuan-note/siyuan","slug":"no-valid-file-paths-to-write-invalid-utf-8-or-pat","errorCode":null,"errorMessage":"no valid file paths to write (invalid UTF-8 or path)","messagePattern":"no valid file paths to write \\(invalid UTF-8 or path\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/util/clipboard_darwin.go","lineNumber":100,"sourceCode":"\t\treturn nil\n\t}\n\t// 分配 C 的 char* 数组，便于传入 Objective-C\n\tcPaths := make([]*C.char, len(paths))\n\tfor i, p := range paths {\n\t\tcPaths[i] = C.CString(p)\n\t}\n\tdefer func() {\n\t\tfor _, c := range cPaths {\n\t\t\tC.free(unsafe.Pointer(c))\n\t\t}\n\t}()\n\t// 取首元素地址作为 const char** 传入\n\tret := C.writeFilePathsToPasteboard((**C.char)(unsafe.Pointer(&cPaths[0])), C.int(len(paths)))\n\tswitch ret {\n\tcase 0:\n\t\treturn nil\n\tcase -2:\n\t\treturn errors.New(\"no valid file paths to write (invalid UTF-8 or path)\")\n\tdefault:\n\t\treturn errors.New(\"failed to write file paths to pasteboard\")\n\t}\n}\n","sourceCodeStart":82,"sourceCodeEnd":105,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/clipboard_darwin.go#L82-L105","documentation":"Returned on macOS when the embedded Objective-C writeFilePathsToPasteboard returns -2, meaning the NSMutableArray of NSURL objects ended up empty. That happens only if every input path produced a nil NSString (invalid UTF-8 bytes) or a nil NSURL (path rejected by fileURLWithPath:). The Go side translates -2 into this message.","triggerScenarios":"Calling WriteFilePaths on macOS with a slice whose strings are all invalid UTF-8, empty, or are not representable as file URLs (e.g. contain embedded NUL bytes or are malformed).","commonSituations":"Asset paths read from a non-UTF-8 filesystem encoding; a corrupt or empty path string passed from the asset API; a path containing a NUL byte after string processing.","solutions":["Validate every path with utf8.ValidString and filepath.Clean before calling WriteFilePaths, and drop empty strings.","Confirm the caller (kernel/api/clipboard.go:90) is passing a resolved absolute path from model.GetAssetAbsPathInBox rather than a relative or malformed one.","Check for NUL bytes or other control characters in the path and reject them upstream."],"exampleFix":"// before\nutil.WriteFilePaths(paths)\n\n// after\nvar clean []string\nfor _, p := range paths {\n    if p == \"\" || !utf8.ValidString(p) || strings.ContainsRune(p, 0) {\n        continue\n    }\n    clean = append(clean, filepath.Clean(p))\n}\nif len(clean) == 0 {\n    return errors.New(\"no valid paths\")\n}\nutil.WriteFilePaths(clean)","handlingStrategy":"validation","validationCode":"func validClipboardPaths(paths []string) bool {\n    for _, p := range paths {\n        if p == \"\" || !utf8.ValidString(p) || strings.ContainsRune(p, 0) {\n            return false\n        }\n    }\n    return len(paths) > 0\n}","typeGuard":null,"tryCatchPattern":"if err := util.WriteFilePaths(paths); err != nil {\n    if err.Error() == \"no valid file paths to write (invalid UTF-8 or path)\" {\n        // surface a clear 'invalid path' message to the user\n    }\n}","preventionTips":["Resolve asset paths through model.GetAssetAbsPathInBox and utf8.ValidString them before writing.","Reject empty strings and NUL-containing strings at the API boundary."],"tags":["clipboard","macos","utf8","cgo"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}