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

Don't return typed nil interface values #46

Merged
merged 1 commit into from
Apr 25, 2016

Conversation

babbageclunk
Copy link
Contributor

@babbageclunk babbageclunk commented Apr 22, 2016

Fix crash in Juju caused by typed nil Link.
https://bugs.launchpad.net/juju-core/+bug/1573659
The line immediately before the panic checked for nil, but because the type pointer is set on the Link it wasn't equal to nil.

From reading about this it seems like methods that return interfaces shouldn't implicitly return nil values, they should return nil explicitly, otherwise the result will be a typed nil. Checking for a typed nil on the client side is awkward - if the underlying type is known and accessible you could try a type assertion, otherwise it requires reflection.

https://golang.org/doc/faq#nil_error
(About errors, but applies to any interface.)

Methods that return interfaces shouldn't implicitly return nil values,
they should return nil explicitly. Otherwise the result is a typed nil,
which will compare != nil but still trigger a nil reference panic if any
methods are called on it.

https://golang.org/doc/faq#nil_error
(About errors, but applies to any interface.)

https://bugs.launchpad.net/juju-core/+bug/1573659

This crash was caused by a typed nil link - the line immediately before
the panic checked for a nil, but because the type pointer is set on the
Link it wasn't equal to nil.
@@ -19,6 +19,11 @@ type deviceSuite struct {

var _ = gc.Suite(&deviceSuite{})

func (*deviceSuite) TestNilZone(c *gc.C) {
var empty device
c.Check(empty.Zone() == nil, jc.IsTrue)

Choose a reason for hiding this comment

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

Or alternatively:

var zeroDevice device
c.Check(zeroDevice.Zone(), gc.IsNil)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried that first, but it uses reflection so didn't fail before I fixed the problem.

@dimitern
Copy link

LGTM, apart from a few suggestions for tests simplifications.

@babbageclunk
Copy link
Contributor Author

$$merge$$

@jujubot
Copy link
Contributor

jujubot commented Apr 25, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju-gomaasapi

@jujubot jujubot merged commit 9ec917c into juju:master Apr 25, 2016
@babbageclunk babbageclunk deleted the nil-interfaces branch April 25, 2016 09:27
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.

3 participants