Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed NPE in Stringer() #528

Closed
wants to merge 4 commits into from
Closed

Conversation

selslack
Copy link

@selslack selslack commented Nov 9, 2017

Fixes #495

@CLAassistant
Copy link

CLAassistant commented Nov 9, 2017

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.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov
Copy link

codecov bot commented Nov 9, 2017

Codecov Report

Merging #528 into master will increase coverage by 0.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #528      +/-   ##
==========================================
+ Coverage   97.06%   97.07%   +0.01%     
==========================================
  Files          39       39              
  Lines        2279     2289      +10     
==========================================
+ Hits         2212     2222      +10     
  Misses         59       59              
  Partials        8        8
Impacted Files Coverage Δ
field.go 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f85c78b...f1e313e. Read the comment docs.

return Skip()
}

r := reflect.ValueOf(val)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have a few concerns with this approach:

  • we're adding a reflect call when creating a field, which could have a performance impact (we should measure the impact)
  • we're changing the behaviour of Stringer. It's possible for a type to implement a String method that works for nil:
    https://play.golang.org/p/EUNoR7Sz9Q

Now, we're changing the behaviour where it'll Skip the field.

This is also bad for nil slices for example, where we might want to output a empty sice rather than skipping the field.

Copy link
Author

Choose a reason for hiding this comment

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

  1. It's 288 ns/op vs 316 ns/op on my laptop, additional datapoints are welcome.

  2. Yea, I'm aware of that possibility. Simplest way of maintaining compatibility is to introduce something like StringerNilSafe field or bump the library version. If you have any ideas -- they are welcome.

I find the behavior of Error simple and useful: if argument is nil it's not rendered.

As for slices: there could be scenario where I want to distinguish nil-slice vs empty slice in logs.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind adding the nil check above; it's cheap and always correct.

I'd prefer not to have the reflection-based checks below. If we get a non-nil interface with a nil value that also dereferences the receiver in String, it's completely reasonable to panic - in my book, that's definitely programmer error.

Copy link
Contributor

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

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

The reflection is a step too far for me. If you'd like to amend the PR to only include the check for nil interfaces, I'm happy to merge it and include it in the next release.

return Skip()
}

r := reflect.ValueOf(val)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't mind adding the nil check above; it's cheap and always correct.

I'd prefer not to have the reflection-based checks below. If we get a non-nil interface with a nil value that also dereferences the receiver in String, it's completely reasonable to panic - in my book, that's definitely programmer error.

@akshayjshah
Copy link
Contributor

😶 Closing due to inactivity. 😶

This is just housekeeping - leave a comment and re-open if necessary!

joa added a commit to joa/zap that referenced this pull request Mar 27, 2019
This commit ensures any panic raised while encoding zap.Stringer
is catched and reported as an error of the form kError
for a field k. The reported panic is prefixed with "PANIC=" to
distinguish these errors from other encoding errors.

Closes uber-go#495
Closes uber-go#528
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

logger.Info(zap.Any("",*url.URL)) should not panic if *url.URL is nil
4 participants