vmware / go-vcloud-director

Golang SDK for VMware Cloud Director
Other
80 stars 76 forks source link

Adding a DNAT rule to an Edge Gateway causes a segmentation fault #585

Open dlipovetsky opened 1 year ago

dlipovetsky commented 1 year ago

Describe the bug

I get a segmentation fault when adding a DNAT rule to an Edge Gateway.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x38 pc=0x69cf45]

goroutine 1 [running]:
github.com/vmware/go-vcloud-director/v2/govcd.(*EdgeGateway).AddNATRuleAsync(0xc000017200, {{0x766b93, 0x4}, {0x0, 0x0}, {0x7687a5, 0xa}, {0x0, 0x0}, {0x7687b9, ...}, ...})
    /go/pkg/mod/github.com/vmware/go-vcloud-director/v2@v2.20.0/govcd/edgegateway.go:474 +0x365
github.com/vmware/go-vcloud-director/v2/govcd.(*EdgeGateway).AddDNATRule(0xc000017200, {{0x766b93, 0x4}, {0x0, 0x0}, {0x7687a5, 0xa}, {0x0, 0x0}, {0x7687b9, ...}, ...})
    /go/pkg/mod/github.com/vmware/go-vcloud-director/v2@v2.20.0/govcd/edgegateway.go:289 +0xc5
main.main()
    main.go:102 +0x458

The root cause appears to be that the AddNATRuleAsync method assumes that EdgeGateway.Configuration.EdgeGatewayServiceConfiguration is not nil, and references its NatService field: https://github.com/vmware/go-vcloud-director/blob/c3b8ae89e81a01b16a51784630ce6d3b23263f99/govcd/edgegateway.go#L469-L474.

Reproduction steps

  1. Add a DNAT rule using this API method https://github.com/vmware/go-vcloud-director/blob/c3b8ae89e81a01b16a51784630ce6d3b23263f99/govcd/edgegateway.go#L280

Expected behavior

A DNAT rule is added to the edge gateway.

Additional context

I have successfully created DNAT rules on the edge gateway using the UI.

VCD version: 10.4.1.21373066 go-vcloud-director version: v2.20.0

adezxc commented 1 year ago

Hi @dlipovetsky,

Thanks for reporting this issue.

You are correct, there is an unchecked condition where the underlying struct is nil. Just to check before creating a fix, could you provide more code that you use for the Edge Gateway as I didn't manage to get a nil structure while fetching one, maybe there are more things to fix in the code.

dlipovetsky commented 1 year ago

@adezxc Sure! https://github.com/dlipovetsky/dnat-rules-example

Thanks for looking into this.