Skip to content
This repository was archived by the owner on Jan 8, 2024. It is now read-only.

Not embedding hclConfig as a pointer. #1710

Merged
merged 2 commits into from
Jun 30, 2021
Merged

Conversation

izaaklauer
Copy link
Contributor

@izaaklauer izaaklauer commented Jun 24, 2021

What problem is this solving?

This addresses the panic within #1704.

The problem here is that, if we embed a pointer to a private struct, and the private struct is nil, the caller has no way to determine if the embedded struct's values are safe to access.

This manifests here:

if c.cfg == nil || c.cfg.Runner == nil || !c.cfg.Runner.Enabled {

There is no way to determine of c.cfg.Runner is safe to access. c.cfg can be non-nil on its own, but it will become nil when inspecting c.cfg.Runner if the embedded *hclConfig is nil.

A simplified example of the problem:

https://play.golang.org/p/sbAPT2HGJqW

If hclConfig is not a pointer, we won't get a nil pointer deference when looking at its fields.

Is this change safe?

I believe so - we don't have any methods on hclConfig that require it to be a pointer. We also never statefully maniuplate it outside of the context of cfg, as far as I can see. I've tested this locally and it doesn't seem to break anything.

This addresses the panic within #1704.

The problem here is that, if you embed a pointer to a private struct, and the private struct is nil, the caller has no way to determine if the embedded struct's values are safe to access.

This manifests here: https://github.com/hashicorp/waypoint/blob/main/internal/cli/base.go#L253

There is no way to determine of c.cfg.Runner is safe to access.

A simplified example of the problem:

https://play.golang.org/p/sbAPT2HGJqW

I believe so - we don't have any methods on hclConfig that require it to be a pointer. We also never statefully maniuplate it outside of the context of `cfg`, as far as I can see. I've tested this locally and it doesn't seem to break anything.
@izaaklauer izaaklauer requested a review from a team June 24, 2021 17:57
@krantzinator krantzinator added this to the 0.4.x milestone Jun 30, 2021
Copy link
Contributor

@evanphx evanphx left a comment

Choose a reason for hiding this comment

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

Because the expectation is that users outside this package are going to be accessing things like Runner on Config, but via hclConfig, it makes sense to have hclConfig be a value and not a pointer because those outside users can't even nil check hclConfig. Ergo it makes sense to just have hclConfig be a value.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants