{"record":{"id":"259368682abdf72d","repo":"hashicorp/nomad","slug":"failed-to-convert-username-to-utf-16-w","errorCode":null,"errorMessage":"failed to convert username to UTF-16: %w","messagePattern":"failed to convert username to UTF-16: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"drivers/shared/executor/executor_windows.go","lineNumber":84,"sourceCode":"\t}, cmd.SysProcAttr)\n\n\treturn nil\n}\n\nvar (\n\tadvapiDll      = windows.NewLazySystemDLL(\"advapi32.dll\")\n\tprocLogonUserW = advapiDll.NewProc(\"LogonUserW\")\n)\n\nconst (\n\t_LOGON_SERVICE    uint32 = 5\n\t_PROVIDER_DEFAULT uint32 = 0\n)\n\nfunc createUserToken(domain, username string) (*syscall.Token, error) {\n\tuserw, err := syscall.UTF16PtrFromString(username)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to convert username to UTF-16: %w\", err)\n\t}\n\tdomainw, err := syscall.UTF16PtrFromString(domain)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to convert user domain to UTF-16: %w\", err)\n\t}\n\tvar token syscall.Token\n\tret, _, e := procLogonUserW.Call(\n\t\tuintptr(unsafe.Pointer(userw)),\n\t\tuintptr(unsafe.Pointer(domainw)),\n\t\tuintptr(unsafe.Pointer(nil)),\n\t\tuintptr(_LOGON_SERVICE),\n\t\tuintptr(_PROVIDER_DEFAULT),\n\t\tuintptr(unsafe.Pointer(&token)),\n\t)\n\tif ret == 0 {\n\t\treturn nil, e\n\t}\n","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/drivers/shared/executor/executor_windows.go#L66-L102","documentation":"createUserToken converts the username to a NUL-terminated UTF-16 pointer required by the Win32 LogonUserW API. syscall.UTF16PtrFromString fails only if the string contains an interior NUL byte (or is too large); the error is wrapped so the caller knows username encoding failed before any logon attempt.","triggerScenarios":"The username passed to createUserToken contains a '\\x00' character — practically only via programmatically supplied user strings rather than normal config.","commonSituations":"Corrupted job spec or templated user value injecting a NUL; binary data misinterpreted as a username from a misbehaving driver/plugin.","solutions":["Inspect the task's user value for embedded NUL/control characters and correct its source.","Sanitize the user string (strip non-printable characters) before passing it to the executor.","Correct the job spec so the username is plain text, e.g. 'DOMAIN\\\\user'."],"exampleFix":"// before\nuser = strings.TrimSuffix(raw, \"\\x00\") // still contains \\x00 inside\n// after\nuser = strings.Map(func(r rune) rune { if r == 0 { return -1 }; return r }, raw)","handlingStrategy":"validation","validationCode":"// Go: reject usernames containing NUL before calling the executor\nif strings.ContainsRune(username, 0) {\n    return errors.New(\"username must not contain NUL bytes\")\n}","typeGuard":"func utf16Safe(s string) bool {\n    _, err := syscall.UTF16PtrFromString(s)\n    return err == nil\n}","tryCatchPattern":"if err := exec.SetUser(cmd, user); err != nil {\n    if strings.Contains(err.Error(), \"convert username to UTF-16\") {\n        return fmt.Errorf(\"malformed username (control chars?) in %q\", user)\n    }\n    return err\n}","preventionTips":["Validate user strings against a printable-character regex at job-submission time.","Avoid concatenating binary/templated data into the user field.","Log the raw user value (quoted) when this error occurs."],"tags":["windows","utf-16","encoding","logonuser"],"backgroundTag":"string-encoding-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}