Skip to content

Commit 2fd9e2b

Browse files
ChrisBQuOvOhao
andauthored
fix: Prevent node from leaking during C binding CloseNode (#5221)
## Relevant issue(s) Resolves #5206 ## Description It was the case that the C bindings' `CloseNode` function would leak the node if the `node.Close` call, for some reason, failed. This was because this failure path leads to the error being returned, and the CGO handle not being deleted. This PR fixes that by moving the delete above the `node.Close` call, and having it deferred. ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? Specify the platform(s) on which this was tested: - WSL Co-authored-by: OvOhao <123258594+OvOhao@users.noreply.github.com>
1 parent 53f0e76 commit 2fd9e2b

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

cbindings/node_close.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ func CloseNode(nodePtr C.uintptr_t) C.Result {
2727
if err != nil {
2828
return returnC(returnGoC(1, err.Error(), ""))
2929
}
30-
err = node.Close(context.Background())
31-
if err != nil {
30+
defer cgo.Handle(nodePtr).Delete()
31+
32+
if err := node.Close(context.Background()); err != nil {
3233
return returnC(GoCResult{1, err.Error(), ""})
3334
}
34-
cgo.Handle(nodePtr).Delete()
3535
return returnC(GoCResult{0, "", ""})
3636
}

0 commit comments

Comments
 (0)