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

Fix nil pointer dereference on initialization #841

Merged
merged 2 commits into from
Sep 22, 2020

Conversation

ophum
Copy link
Contributor

@ophum ophum commented Sep 18, 2020

Signed-off-by: ophum [email protected]

Please excuse my poor English.

Summary

line 116 in cmd/cmd.go
will happen invalid memory address or nil pointer dereference when err is not nil.
because client is nil

unuse returned client. use empty pack.Client.

Output

Before

After

Documentation

  • Should this change be documented?
    • Yes, see #___
    • No

Related

Resolves #___

@ophum ophum requested a review from a team as a code owner September 18, 2020 06:22
@jromero
Copy link
Member

jromero commented Sep 21, 2020

Hi @ophum, I'm curious as to how you came about this error. In Go, it's common practice to not use the non-error return values when error is not nil. In other words, this is expected.

Let me know if I'm missing something.

References:

@ophum
Copy link
Contributor Author

ophum commented Sep 22, 2020

@jromero thank you for your reply.

Will happen error when following command is execute.

$ DOCKER_HOST=192.168.10.1:2375 pack build imageName
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb2ef61]

goroutine 1 [running]:
github.com/buildpacks/pack/cmd.initClient(0x7f4384b63250, 0xc00029aaf0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
        github.com/buildpacks/pack@/cmd/cmd.go:116 +0xd1
github.com/buildpacks/pack/cmd.NewPackCommand(0xe308e0, 0xc00029aaf0, 0xe123c0, 0xc000285e00, 0x0)
        github.com/buildpacks/pack@/cmd/cmd.go:31 +0x1b0
main.main()
        github.com/buildpacks/pack@/cmd/pack/main.go:19 +0x9a

192.168.10.1:2375 is docker daemon.
Because DOCKER_HOST must be assign in format of tcp://192.168.10.1:2375.

Error of pack.NewClient() is errors.Wrap(err, "creating docker client") line 146 in pack/client.go at this time.
So that error is returned on line 116 in pack/cmd/cmd.go .
But *client is returned on line 116 in pack/cmd/cmd.go.
Will happen invalid memory address or nil pointer dereference at this time, because client is nil when err is not nil.

So I changed return *client, err to return pack.Client{}, err.

$ DOCKER_HOST=192.168.10.1:2375 pack build imageName
ERROR: creating docker client: unable to parse docker host `192.168.10.1:2375`

@jromero jromero added the type/bug Issue that reports an unexpected behaviour. label Sep 22, 2020
@jromero jromero added this to the 0.14.0 milestone Sep 22, 2020
Copy link
Member

@jromero jromero left a comment

Choose a reason for hiding this comment

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

Ah! I see now! Thank you for taking the time to dig in and explain it to me. 🙏

@jromero jromero merged commit 9a2ac93 into buildpacks:main Sep 22, 2020
@jromero jromero changed the title fix nil pointer dereference Fix nil pointer dereference on initialization Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Issue that reports an unexpected behaviour.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants