Remove potential nil ptr dereferences#1223
Conversation
|
This issue is currently awaiting triage. If cloud-provider-aws contributors determine this is a relevant issue, they will accept it by applying the The DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
|
Hi @gargipanatula. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. |
c92e7d2 to
a626bc0
Compare
| if !errors.As(err, ¬FoundErr) { | ||
| return fmt.Errorf("error describing security policies on load balancer: %q", err) | ||
| } | ||
| return nil |
There was a problem hiding this comment.
we shouldn't return here as PolicyNotFoundException would mean we have to create the policy
There was a problem hiding this comment.
Ah thank you - pushed up a fix so that the code changes from
result, err := c.elb.DescribeLoadBalancerPolicies(...)
if err != nil {
var notFoundErr *elbtypes.PolicyNotFoundException
if !errors.As(err, ¬FoundErr) {
return fmt.Errorf("error describing security policies on load balancer: %q", err)
}
}
if len(result.PolicyDescriptions) > 0 {
return nil
}
to
result, err := c.elb.DescribeLoadBalancerPolicies(...)
policyNotFoundError := false
if err != nil {
var notFoundErr *elbtypes.PolicyNotFoundException
if !errors.As(err, ¬FoundErr) {
return fmt.Errorf("error describing security policies on load balancer: %q", err)
}
policyNotFoundError = true
}
if (!policyNotFoundError) && (result != nil && len(result.PolicyDescriptions) > 0) {
// if policynotfound, would return false
return nil
}
This maintains the behavior that was present in V1, where the following happened:
- If
DescribeLoadBalancerPoliciesreturnsPolicyNotFoundException, the check will return false (result will be an empty struct soresult.PolicyDescriptionswill be 0) - If
DescribeLoadBalancerPoliciesreturns any other error, the check will return false (result will be an empty struct soresult.PolicyDescriptionswill be 0) - If
DescribeLoadBalancerPoliciesdoes not return an error, the check will be evaluated as normal.
In V2, with these changes, it would look like this:
- If
DescribeLoadBalancerPoliciesreturnsPolicyNotFoundException, the check will return false (policyNotFoundErrorcheck) - If
DescribeLoadBalancerPoliciesreturns any other error, the check will return false (result will benil) - If
DescribeLoadBalancerPoliciesdoes not return an error, the check will be evaluated as normal (not apolicyNotFoundErrorandresult != nil).
| } else { | ||
| instance, err := c.getInstanceByID(ctx, string(instanceID)) | ||
| if err != nil { | ||
| if err != nil || instance == nil { |
There was a problem hiding this comment.
can this happen where err is nil and instance is also nil?
There was a problem hiding this comment.
Yeah, getInstanceByID does
return instances[instanceID], nil
and instances[instanceID] can be nil if instanceID doesn't exist in the instances map
|
|
||
| instance, err := c.getInstanceByID(ctx, string(instanceID)) | ||
| if err != nil { | ||
| if err != nil || instance == nil { |
There was a problem hiding this comment.
When is it ok for the instance to be nil, and for there to be no error?
There was a problem hiding this comment.
I don't think it's ever ok for instance to be nil.
This is because if instance is nil and not caught, it will immediately get passed into getInstanceZone (line 1010) where it will produce a nil ptr dereference issue because getInstanceZone dereferences instance without checking if it's nil.
getInstanceZone code:
func (c *Cloud) getInstanceZone(instance *ec2.Instance) cloudprovider.Zone {
return cloudprovider.Zone{
FailureDomain: *(instance.Placement.AvailabilityZone),
Region: c.region,
}
}
There was a problem hiding this comment.
I think I misunderstood your question, if we're wondering when we get an output like nil, nil, getInstanceByID can produce this output, hence the need for if err != nil || instance == nil {.
4f4b481 to
0285ce6
Compare
| } else { | ||
| instance, err = c.getInstanceByID(ctx, string(awsID)) | ||
| } | ||
| if err == nil && instance == nil { |
There was a problem hiding this comment.
For context, the only case where this would've been true is if we had hit the
return instances[instanceID], nil
case in getInstanceByID, and instances[instanceID] was nil.
With these changes, if instances[instanceID] is nil, we now throw an InstanceNotFound error. So, instead of checking if err == nil && instance == nil, we can just check if err != nil and throw the resulting error from the function.
| // | ||
| // The result of DescribeLoadBalancerPolicies will be nil, so we should only check | ||
| // result.PolicyDescriptions if DescribeLoadBalancerPolicies did not yield an error. | ||
| if (!policyNotFoundError) && (result != nil && len(result.PolicyDescriptions) > 0) { |
There was a problem hiding this comment.
is there a need for policyNotFoundError? we can just have (result != nil && len(result.PolicyDescriptions) > 0) right if the result != nil anyway means we don't have any error?
bd1b727 to
2b18d4d
Compare
|
/ok-to-test |
|
/lgtm |
|
/approve |
64f49a4 to
cc8d288
Compare
|
/lgtm |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: kmala The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
…#1223-upstream-release-1.32 Automated cherry pick of #1223: remove nil ptr dereference
…#1223-upstream-release-1.34 Automated cherry pick of #1223: remove nil ptr dereference
…#1223-upstream-release-1.33 Automated cherry pick of #1223: remove nil ptr dereference
(1.31) Automated cherry pick of #1223: Remove potential nil ptr dereferences
Automated cherry pick of #1223: Remove potential nil ptr dereferences
What type of PR is this?
/kind bug
What this PR does / why we need it:
Fixes a nil pointer dereference introduced by the SDK Go V2 migration in #1157.
The SDK migration's nil pointer dereference issue is in pkg/providers/v1/aws_loadbalancer.go:ensureSSLNegotiationPolicy, and is due to the SDK Go V2 returning nil on error rather than an empty struct. So, when the code didn't return an error and continued into the code, the nil pointer dereference surfaced.
V1 code: https://github.com/aws/aws-sdk-go/blob/main/service/elb/api.go#L1530-L1564
V2 code: https://github.com/aws/aws-sdk-go-v2/blob/service/elasticloadbalancing/v1.31.0/service/elasticloadbalancing/api_op_DescribeLoadBalancerPolicies.go#L29
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Does this PR introduce a user-facing change?: