{"record":{"id":"4f4afb1b327e74cf","repo":"wailsapp/wails","slug":"keystate-slice-must-have-a-size-of-256-bytes","errorCode":null,"errorMessage":"keyState slice must have a size of 256 bytes","messagePattern":"keyState slice must have a size of 256 bytes","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"v3/pkg/w32/user32.go","lineNumber":1267,"sourceCode":"\treturn ret != 0\n}\n\nfunc BeginPaint(hwnd HWND, paint *PAINTSTRUCT) HDC {\n\tret, _, _ := procBeginPaint.Call(\n\t\tuintptr(hwnd),\n\t\tuintptr(unsafe.Pointer(paint)))\n\treturn HDC(ret)\n}\n\nfunc EndPaint(hwnd HWND, paint *PAINTSTRUCT) {\n\tprocEndPaint.Call(\n\t\tuintptr(hwnd),\n\t\tuintptr(unsafe.Pointer(paint)))\n}\n\nfunc GetKeyboardState(keyState []byte) bool {\n\tif len(keyState) != 256 {\n\t\tpanic(\"keyState slice must have a size of 256 bytes\")\n\t}\n\tret, _, _ := procGetKeyboardState.Call(uintptr(unsafe.Pointer(&keyState[0])))\n\treturn ret != 0\n}\n\nfunc MapVirtualKeyEx(uCode, uMapType uint, dwhkl HKL) uint {\n\tret, _, _ := procMapVirtualKeyEx.Call(\n\t\tuintptr(uCode),\n\t\tuintptr(uMapType),\n\t\tuintptr(dwhkl))\n\treturn uint(ret)\n}\n\nfunc MapVirtualKey(uCode uint, uMapType uint) uint {\n\tret, _, _ := procMapVirtualKey.Call(uintptr(uCode), uintptr(uMapType))\n\treturn uint(ret)\n}\n","sourceCodeStart":1249,"sourceCodeEnd":1285,"githubUrl":"https://github.com/wailsapp/wails/blob/0e754b1b40ba9044c2a1460e23b7c3de20fc5cf3/v3/pkg/w32/user32.go#L1249-L1285","documentation":"GetKeyboardState panics unless the caller passes a []byte of exactly length 256, because the underlying Win32 API fills a fixed 256-entry key-state array indexed by virtual-key code. This is a defensive precondition check inside the wrapper, not a system failure.","triggerScenarios":"Calling w32.GetKeyboardState(keyState) where keyState was allocated with make([]byte, 128), with a dynamic size, or as an empty slice; reusing a struct-embedded slice that was truncated.","commonSituations":"Keyboard-accelerator or shortcut handling code copied from samples that pass arbitrary buffers; passing a pointer into a larger struct cast to a short slice.","solutions":["Allocate exactly 256 bytes: keyState := make([]byte, 256)","Reuse a single package-level [256]byte array across calls instead of reallocating","Add a compile-time-visible constant or helper so callers cannot guess wrong"],"exampleFix":"// before\nkeys := make([]byte, 128) // wrong size\nw32.GetKeyboardState(keys)\n\n// after\nkeys := make([]byte, 256) // VK array is 256 entries\nw32.GetKeyboardState(keys)","handlingStrategy":"type-guard","validationCode":"if len(keyState) != 256 {\n    return false, fmt.Errorf(\"need 256 bytes, got %d\", len(keyState))\n}\nreturn w32.GetKeyboardState(keyState), nil","typeGuard":"func isValidKeyStateBuffer(b []byte) bool { return len(b) == 256 }","tryCatchPattern":null,"preventionTips":["Standardize on make([]byte, 256) or a [256]byte array in every caller","Wrap the API once in your own helper that allocates correctly"],"tags":["windows","win32","keyboard","precondition","panic"],"backgroundTag":null,"analyzedSha":"0e754b1b40ba9044c2a1460e23b7c3de20fc5cf3","analyzedAt":"2026-08-15T14:17:36.034Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}