Skip to content

Remove potential nil ptr dereferences#1223

Merged
k8s-ci-robot merged 1 commit into
kubernetes:masterfrom
gargipanatula:handle-elb-error
Aug 20, 2025
Merged

Remove potential nil ptr dereferences#1223
k8s-ci-robot merged 1 commit into
kubernetes:masterfrom
gargipanatula:handle-elb-error

Conversation

@gargipanatula
Copy link
Copy Markdown
Contributor

@gargipanatula gargipanatula commented Aug 12, 2025

What type of PR is this?

Uncomment only one, leave it on its own line:

/kind api-change

/kind bug

/kind cleanup
/kind design
/kind documentation
/kind failing-test
/kind feature
/kind flake

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?:

NONE

@k8s-ci-robot k8s-ci-robot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Aug 12, 2025
@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Aug 12, 2025
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If cloud-provider-aws contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions 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.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Aug 12, 2025
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions 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.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Aug 12, 2025
Comment thread pkg/providers/v1/aws_loadbalancer.go Outdated
if !errors.As(err, &notFoundErr) {
return fmt.Errorf("error describing security policies on load balancer: %q", err)
}
return nil
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we shouldn't return here as PolicyNotFoundException would mean we have to create the policy

Copy link
Copy Markdown
Contributor Author

@gargipanatula gargipanatula Aug 12, 2025

Choose a reason for hiding this comment

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

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, &notFoundErr) {
			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, &notFoundErr) {
			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 DescribeLoadBalancerPolicies returns PolicyNotFoundException, the check will return false (result will be an empty struct so result.PolicyDescriptions will be 0)
  • If DescribeLoadBalancerPolicies returns any other error, the check will return false (result will be an empty struct so result.PolicyDescriptions will be 0)
  • If DescribeLoadBalancerPolicies does not return an error, the check will be evaluated as normal.

In V2, with these changes, it would look like this:

  • If DescribeLoadBalancerPolicies returns PolicyNotFoundException, the check will return false (policyNotFoundError check)
  • If DescribeLoadBalancerPolicies returns any other error, the check will return false (result will be nil)
  • If DescribeLoadBalancerPolicies does not return an error, the check will be evaluated as normal (not a policyNotFoundError and result != nil).

Comment thread pkg/providers/v1/instances_v2.go Outdated
} else {
instance, err := c.getInstanceByID(ctx, string(instanceID))
if err != nil {
if err != nil || instance == nil {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can this happen where err is nil and instance is also nil?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, getInstanceByID does

return instances[instanceID], nil

and instances[instanceID] can be nil if instanceID doesn't exist in the instances map

Comment thread pkg/providers/v1/aws.go Outdated

instance, err := c.getInstanceByID(ctx, string(instanceID))
if err != nil {
if err != nil || instance == nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

When is it ok for the instance to be nil, and for there to be no error?

Copy link
Copy Markdown
Contributor Author

@gargipanatula gargipanatula Aug 12, 2025

Choose a reason for hiding this comment

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

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,
	}
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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 {.

@gargipanatula gargipanatula force-pushed the handle-elb-error branch 2 times, most recently from 4f4b481 to 0285ce6 Compare August 18, 2025 21:35
Comment thread pkg/providers/v1/aws.go
} else {
instance, err = c.getInstanceByID(ctx, string(awsID))
}
if err == nil && instance == nil {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread pkg/providers/v1/aws_loadbalancer.go Outdated
//
// 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) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

@kmala
Copy link
Copy Markdown
Member

kmala commented Aug 20, 2025

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 20, 2025
@kmala
Copy link
Copy Markdown
Member

kmala commented Aug 20, 2025

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 20, 2025
@kmala
Copy link
Copy Markdown
Member

kmala commented Aug 20, 2025

/approve

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Aug 20, 2025
@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed lgtm "Looks good to me", indicates that a PR is ready to be merged. labels Aug 20, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Aug 20, 2025
@kmala
Copy link
Copy Markdown
Member

kmala commented Aug 20, 2025

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 20, 2025
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[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

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot merged commit 83361d0 into kubernetes:master Aug 20, 2025
11 checks passed
k8s-ci-robot added a commit that referenced this pull request Aug 21, 2025
…#1223-upstream-release-1.32

Automated cherry pick of #1223: remove nil ptr dereference
k8s-ci-robot added a commit that referenced this pull request Aug 21, 2025
…#1223-upstream-release-1.34

Automated cherry pick of #1223: remove nil ptr dereference
k8s-ci-robot added a commit that referenced this pull request Aug 21, 2025
…#1223-upstream-release-1.33

Automated cherry pick of #1223: remove nil ptr dereference
k8s-ci-robot added a commit that referenced this pull request Nov 10, 2025
(1.31) Automated cherry pick of #1223: Remove potential nil ptr dereferences
k8s-ci-robot added a commit that referenced this pull request Nov 13, 2025
Automated cherry pick of #1223: Remove potential nil ptr dereferences
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants