{"record":{"id":"d33d4b31712a2ddc","repo":"go-delve/delve","slug":"wrong-response-length-expected-d-got-d","errorCode":null,"errorMessage":"wrong response length, expected %d got %d","messagePattern":"wrong response length, expected (.+?) got (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/gdbserial/gdbserver_conn.go","lineNumber":538,"sourceCode":"}\n\n// readRegister executes 'p' (read register) command.\nfunc (conn *gdbConn) readRegister(threadID string, regnum int, data []byte) error {\n\tif !conn.threadSuffixSupported {\n\t\tif err := conn.selectThread('g', threadID, \"registers write\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tconn.outbuf.Reset()\n\tfmt.Fprintf(&conn.outbuf, \"$p%x\", regnum)\n\tconn.appendThreadSelector(threadID)\n\tresp, err := conn.exec(conn.outbuf.Bytes(), \"register read\")\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif len(resp) > len(data)*2 {\n\t\treturn fmt.Errorf(\"wrong response length, expected %d got %d\", len(data)*2, len(resp))\n\t}\n\n\tfor i := 0; i < len(resp); i += 2 {\n\t\tn, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)\n\t\tdata[i/2] = uint8(n)\n\t}\n\n\treturn nil\n}\n\n// writeRegister executes 'P' (write register) command.\nfunc (conn *gdbConn) writeRegister(threadID string, regnum int, data []byte) error {\n\tif !conn.threadSuffixSupported {\n\t\tif err := conn.selectThread('g', threadID, \"registers write\"); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tconn.outbuf.Reset()","sourceCodeStart":520,"sourceCodeEnd":556,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/gdbserial/gdbserver_conn.go#L520-L556","documentation":"This error is thrown by gdbConn.readRegister when the GDB remote stub returns a hex-encoded register value longer than the expected 2*len(data) hex characters, i.e. the target sent more bytes than the destination buffer can hold. Delve validates the response length before decoding to avoid overflowing the caller-provided byte slice. It indicates the remote stub answered a register-read ('p' packet) with an unexpected register size, usually because stub and client disagree on register width.","triggerScenarios":"Calling conn.readRegister (via thread registers access) against a gdbserial target whose stub returns more hex bytes than the register width Delve expects — e.g. the 'p' reply contains extra bytes or the stub ignores the register number and returns a different register.","commonSituations":"Debugging with a non-standard or buggy gdbserver/stub (openocd, custom RTOS stubs, QEMU with unusual register sets); architecture mismatches between Delve's expected register layout and the target's actual layout; connecting to stubs that don't implement 'p' correctly.","solutions":["Update or replace the gdbserver/stub so it returns the exact number of bytes for the requested register","Verify the target architecture matches the Delve build (GOARCH) — register sizes must agree","Use the 'g' (read all registers) path or a stub that supports proper register enumeration","Check Delve version and upgrade; register-width handling for exotic stubs is fixed over time"],"exampleFix":"// before: buffer sized for expected register, stub returns extra bytes\ndata := make([]byte, 8)\n// stub replied with 10 bytes -> error\n\n// after: pad/truncate the response before calling the lower layer, or request\n// a register set whose width matches the stub, e.g. use ReadRegisters (g packet)\n// which allocates from the reply length instead of a fixed buffer","handlingStrategy":"validation","validationCode":"// Before relying on a register read against a gdbserial target, confirm the stub\n// honors 'p' with the expected width:\nresp, err := conn.exec([]byte(\"p\" + hexRegNum), \"register read\")\nif err != nil { return err }\nif len(resp) != len(data)*2 {\n    return fmt.Errorf(\"stub register width mismatch: got %d hex chars, want %d\", len(resp), len(data)*2)\n}","typeGuard":null,"tryCatchPattern":"if err := conn.readRegister(regNum, data); err != nil {\n    if strings.Contains(err.Error(), \"wrong response length\") {\n        // fall back to bulk 'g' register read instead of per-register 'p'\n        return conn.readAllRegisters()\n    }\n    return err\n}","preventionTips":["Match Delve's GOARCH to the target architecture exactly","Test against your stub (openocd/QEMU/custom) with a known register before production use","Prefer stubs that implement the 'g' bulk register read","Keep Delve and the remote stub versions aligned"],"tags":["gdb-serial-protocol","go","register-read","protocol-mismatch"],"backgroundTag":"gdb-remote-register-width-mismatch","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}