Skip to content

fix: avoid panic when ToStringE receives nil Stringer pointer#346

Open
leno23 wants to merge 1 commit into
spf13:masterfrom
leno23:fix/tostring-nil-pointer-stringer
Open

fix: avoid panic when ToStringE receives nil Stringer pointer#346
leno23 wants to merge 1 commit into
spf13:masterfrom
leno23:fix/tostring-nil-pointer-stringer

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 25, 2026

Summary

  • Fixes a panic when ToStringE is called with a nil pointer whose type implements fmt.Stringer (reported for *time.Time in Bug or bad entry ? #227)
  • Guard the fmt.Stringer branch so nil pointer/interface receivers return "" instead of calling String() on a nil receiver

Test plan

  • Added nil *time.Time case to TestString
  • go test ./...
  • Manual repro: cast.ToStringE((*time.Time)(nil)) returns "" without panic

Fixes #227

Return an empty string for nil pointer values that implement
fmt.Stringer (e.g. *time.Time) instead of calling String on a nil
receiver, which panics.

Fixes spf13#227

Co-authored-by: Cursor <cursoragent@cursor.com>
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


wuyangfan seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

Bug or bad entry ?

2 participants