Skip to content

handle typed nil pointers in Encode method (apache/dubbo-go#2517)#376

Open
Nexusrex18 wants to merge 1 commit intoapache:masterfrom
Nexusrex18:issue-2517-2
Open

handle typed nil pointers in Encode method (apache/dubbo-go#2517)#376
Nexusrex18 wants to merge 1 commit intoapache:masterfrom
Nexusrex18:issue-2517-2

Conversation

@Nexusrex18
Copy link
Copy Markdown

This PR fixes the issue where the Encode method in encode.go did not properly handle typed nil pointers (e.g., (*int32)(nil)). The changes ensure that such pointers are encoded as Hessian NULL ('N').

Changes:

  • Modified encode.go to add a reflect-based check for typed nil pointers.
  • Added tests in encode_test.go to verify the behavior for typed nil pointers, untyped nil, and non-nil pointers.
  • Fixed TestUserDefinedException in decode_test.go by:
  • Updating getJavaReply to handle non-zero exit statuses from the Java program.
  • Correcting the Java class name to com.test.UserDefinedException and updating TestThrowable.java accordingly.

Fixes apache/dubbo-go#2517 (Ref: apache/dubbo-go#2517)

This PR is a prerequisite for the corresponding changes in apache/dubbo-go.

cmd := exec.Command("java", cmdArgs...)
out, err := cmd.Output()
if err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why ignore the the exec.ExitError?


func TestEncodeTypedNilPointer(t *testing.T) {
encoder := NewEncoder()
var val *int32 = nil
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we define *int32 as java.lang.Integer,

got, err := decodeJavaResponse(`customReplyJavaIntegerArray`, ``, false)

@mochengqian
Copy link
Copy Markdown
Contributor

@mochengqian

Copy link
Copy Markdown
Contributor

@AlexStocks AlexStocks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review: handle typed nil pointers in Encode method (apache/dubbo-go#2517)

评价

问题定位准确,修复方案正确。解决了 (*int32)(nil) 这类 typed nil pointer 无法正确编码为 Hessian null 的问题。

[P1] 规范级问题

1. encode.go:102-109 - 性能开销

使用 reflect.ValueOf(v).Kind()IsNil() 会增加性能开销。对于高频调用的 Encode 方法,建议使用类型断言优化:

// 优化方案:优先使用类型断言
switch v.(type) {
case nil:
    e.buffer = EncNull(e.buffer)
    return nil
default:
    vVal := reflect.ValueOf(v)
    if vVal.Kind() == reflect.Ptr && vVal.IsNil() {
        e.buffer = EncNull(e.buffer)
        return nil
    }
}
// 继续原有类型判断...

2. 与 PR #384 的关系

本 PR 与 #384 解决同一问题且修改相同文件,建议:

  • 合并两个 PR
  • 保留更完整的测试覆盖

3. 测试覆盖

encode_test.go 的测试覆盖了主要类型,但建议补充:

  • 嵌套指针类型(**int32
  • 接口类型的 nil 判断
  • 结构体指针的 nil 判断

总体评价:修复正确有效,建议合并 #376#384,并补充嵌套指针类型测试。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dubbo protocol and dubbo-go-hessian2 can not process (*int32)(nil) corner case

4 participants