go runtime_error ai_generated true

panic: 运行时错误:无效的内存地址或 nil 指针解引用

panic: runtime error: invalid memory address or nil pointer dereference

ID: go/grpc-unary-interceptor-nil-response

其他格式: JSON · Markdown 中文 · English
80%修复率
83%置信度
0证据数
2025-03-27首次发现

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

一元拦截器返回了 nil 错误但将回复消息留为 nil,或处理函数返回了 nil 响应和 nil 错误。调用方随后解引用该 nil 回复。

English

A unary interceptor returned nil error but left the reply message nil, or the handler returned a nil response with a nil error. The caller then dereferences the nil reply.

generic

解决方案

  1. 92% 成功率
    Ensure handlers always return a non-nil message with nil error: return &pb.UserResponse{...}, nil; and validate in interceptors that reply != nil before returning.
  2. 85% 成功率
    Use protoc-gen-validate or manual checks to guarantee non-nil responses, and add a unit test that asserts the handler never returns (nil, nil).

无效尝试

常见但无效的做法:

  1. 80% 失败

    Recovering hides the nil reply and returns a zero-value to the caller, which then processes garbage.

  2. 75% 失败

    The panic may occur inside the server handler or interceptor chain before the response is sent.