Skip to content

bug: ToString(nil) can panic due to missing nil/validity check before reflect.Value.Kind() #204

@coderabbitai

Description

@coderabbitai

Description

In convert.go, the ToString function's default branch calls reflect.ValueOf(arg).Kind() without first checking whether the reflect.Value is valid. Passing nil as the argument causes reflect.ValueOf(nil) to return an invalid (zero) reflect.Value, and the subsequent call to .Kind() (or .IsNil()) panics at runtime.

Affected code

File: convert.go, ToString function, default branch (around the pointer-kind check).

Expected behavior

ToString(nil) should return "" (or another well-defined zero value) without panicking.

Suggested fix

Add a nil/validity guard before calling Kind():

default:
    if arg == nil {
        return 
    }
    rv := reflect.ValueOf(arg)
    if !rv.IsValid() {
        return 
    }
    kind := rv.Kind()
    if kind == reflect.Pointer && !rv.IsNil() {

References

Metadata

Metadata

Assignees

Labels

☢️ BugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions