You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@opendal.apache.org by yu...@apache.org on 2025/07/10 03:51:17 UTC

(opendal) 01/01: fix(bindings/go): ffi calls use after free

This is an automated email from the ASF dual-hosted git repository.

yuchanns pushed a commit to branch fix-go-use-after-free
in repository https://gitbox.apache.org/repos/asf/opendal.git

commit 540434d9586a884e19feeb03fa949465d9a4b9f0
Author: Hanchin Hsieh <me...@yuchanns.xyz>
AuthorDate: Thu Jul 10 11:51:06 2025 +0800

    fix(bindings/go): ffi calls use after free
---
 bindings/go/opendal.go | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/bindings/go/opendal.go b/bindings/go/opendal.go
index ab2777878..02845bd28 100644
--- a/bindings/go/opendal.go
+++ b/bindings/go/opendal.go
@@ -175,6 +175,13 @@ func NewOperator(scheme Scheme, opts OperatorOptions) (op *Operator, err error)
 	if err != nil {
 		return
 	}
+	defer func() {
+		if err != nil {
+			// cancel must be called after any ffi calls
+			// to prevent `use after free` panics.
+			cancel()
+		}
+	}()
 
 	options := ffiOperatorOptionsNew.symbol(ctx)()
 	setOptions := ffiOperatorOptionsSet.symbol(ctx)
@@ -190,7 +197,6 @@ func NewOperator(scheme Scheme, opts OperatorOptions) (op *Operator, err error)
 
 	inner, err := ffiOperatorNew.symbol(ctx)(scheme, options)
 	if err != nil {
-		cancel()
 		return
 	}