{"record":{"id":"4db8beb796a949b7","repo":"fyne-io/fyne","slug":"tonativeptr-failed-s-s","errorCode":null,"errorMessage":"toNativePtr() failed \"%s\": %s","messagePattern":"toNativePtr\\(\\) failed \"(.+?)\": (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/driver/glfw/driver_windows.go","lineNumber":36,"sourceCode":"\tMB_ICONERROR MB = 0x0000_0010\n\n\tES_CONTINUOUS       ES = 0x80000000\n\tES_DISPLAY_REQUIRED ES = 0x00000002\n)\n\nvar (\n\tkernel32 = syscall.NewLazyDLL(\"kernel32.dll\")\n\tuser32   = syscall.NewLazyDLL(\"user32.dll\")\n\n\texecutionState     = kernel32.NewProc(\"SetThreadExecutionState\")\n\tMessageBox         = user32.NewProc(\"MessageBoxW\")\n\tgetDoubleClickTime = user32.NewProc(\"GetDoubleClickTime\")\n)\n\nfunc toNativePtr(s string) *uint16 {\n\tpstr, err := syscall.UTF16PtrFromString(s)\n\tif err != nil {\n\t\tpanic(fmt.Sprintf(\"toNativePtr() failed \\\"%s\\\": %s\", s, err))\n\t}\n\treturn pstr\n}\n\n// https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw\nfunc messageBoxError(text, caption string) {\n\tuType := MB_OK | MB_ICONERROR\n\n\tsyscall.SyscallN(MessageBox.Addr(),\n\t\tuintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(toNativePtr(text))),\n\t\tuintptr(unsafe.Pointer(toNativePtr(caption))), uintptr(uType))\n}\n\nfunc logError(msg string, err error) {\n\ttext := fmt.Sprintf(\"Fyne error: %v\", msg)\n\tif err != nil {\n\t\ttext = text + fmt.Sprintf(\"\\n  Cause:%v\", err)\n\t}","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/fyne-io/fyne/blob/8860ee95c356385effa5a7be51adb11c6be9d2b6/internal/driver/glfw/driver_windows.go#L18-L54","documentation":"toNativePtr converts Go strings to NUL-terminated UTF-16 for Windows APIs - specifically MessageBoxW, which the glfw driver uses to surface init failures on Windows. syscall.UTF16PtrFromString fails exactly when the string contains an embedded NUL (U+0000), because NUL is the terminator; the helper panics, masking the original init error it was about to display.","triggerScenarios":"A Windows driver-init failure path reaching messageBoxError where the error message or caption contains a literal NUL byte - errors built from binary data, truncated reads, or control characters in log text.","commonSituations":"Error strings concatenated from raw bytes (file/network parsing); binary formats leaking NULs into messages; rare overall, but when hit the dialog never appears and the panic hides the real cause.","solutions":["Sanitize text before it can reach error display: strip NUL bytes (strings.ReplaceAll(s, \"\\x00\", \"\"))","Fix the origin producing NULs in the message (validate binary input, trim buffers)","On Windows, read the stderr/fyne log to see the underlying init error instead of relying on the message box"],"exampleFix":"// before\nmsg := fmt.Sprintf(\"window creation failed: %s\", string(rawBytes)) // may contain NUL\n\n// after\nmsg := \"window creation failed: \" + strings.ReplaceAll(string(rawBytes), \"\\x00\", \"\")","handlingStrategy":"validation","validationCode":"// Pre-flight: make strings safe for Windows wide-char APIs before display.\nfunc displaySafe(s string) string { return strings.ReplaceAll(s, \"\\x00\", \"\") }\n\nif strings.IndexByte(msg, 0) >= 0 { msg = displaySafe(msg) }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize error text built from raw bytes or binary input before logging/displaying","Never build user-facing strings by concatenating unchecked byte slices","On Windows, also check the fyne stderr log when the error dialog fails to appear"],"tags":["windows","syscall","utf16","error-handling","gui","go"],"backgroundTag":null,"analyzedSha":"8860ee95c356385effa5a7be51adb11c6be9d2b6","analyzedAt":"2026-08-15T22:01:51.624Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}