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

check for nil pointers before calling IsZero. #67

Merged
merged 3 commits into from Mar 28, 2023

Conversation

mszczygiel
Copy link
Contributor

@mszczygiel mszczygiel commented Mar 23, 2023

This avoids panics when a nil pointer to a type that implements zeroable interface is passed to IsZero

fixes #66

This avoids panics when a nil pointer to a type that implements zeroable interface is passed to IsZero

Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
@mszczygiel mszczygiel marked this pull request as ready for review March 23, 2023 11:16
util.go Outdated
v := reflect.ValueOf(data)
// check for nil data
switch v.Kind() {
case reflect.Interface, reflect.Ptr:

Choose a reason for hiding this comment

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

How about also checking for reflect.Slice and removing the (now duplicated) check below then?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed, thanks!

util_test.go Outdated
Comment on lines 494 to 510
type dummyZeroable struct {
zero bool
}

func (d dummyZeroable) IsZero() bool {
return d.zero
}
func TestZeroableNilIsZero(t *testing.T) {
var d *dummyZeroable

assert.True(t, IsZero(d))
}

func TestZeroableInterfaceIsRespected(t *testing.T) {
assert.False(t, IsZero(dummyZeroable{false}))
assert.True(t, IsZero(dummyZeroable{true}))
}

Choose a reason for hiding this comment

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

Can't we integrate this is the existing TestIsZero test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we can. Moved, thanks!

@codecov
Copy link

codecov bot commented Mar 25, 2023

Codecov Report

Merging #67 (d2ff6fd) into master (0579829) will increase coverage by 0.10%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master      #67      +/-   ##
==========================================
+ Coverage   85.49%   85.59%   +0.10%     
==========================================
  Files          13       13              
  Lines        1565     1569       +4     
==========================================
+ Hits         1338     1343       +5     
+ Misses        194      193       -1     
  Partials       33       33              
Impacted Files Coverage Δ
util.go 100.00% <100.00%> (+0.41%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
Signed-off-by: Michał Szczygieł <1153719+mszczygiel@users.noreply.github.com>
Copy link

@piepmatz piepmatz left a comment

Choose a reason for hiding this comment

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

thx!

@youyuanwu
Copy link
Member

Looks good. Thanks.

@youyuanwu youyuanwu merged commit f28dd7a into go-openapi:master Mar 28, 2023
8 checks passed
@mszczygiel mszczygiel deleted the fix-is-zero branch March 31, 2023 06:50
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.

IsZero() panics with nil *strfmt.DateTime
3 participants