Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: memory leak if the value is an empty string #262

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/ten_runtime/binding/go/interface/ten/prop.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ func getPropStr(
retrieve func(unsafe.Pointer) C.ten_go_status_t,
) (string, error) {
if size == 0 {
return "", nil
panic("Should not happen.")
}

// About the size of the slice:
Expand Down Expand Up @@ -525,7 +525,7 @@ func getPropBytes(
retrieve func(unsafe.Pointer) C.ten_go_status_t,
) ([]byte, error) {
if size == 0 {
return nil, nil
panic("Should not happen.")
}

buf := acquireBytes(int(size))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,13 @@ func (p *tenEnv) GetPropertyString(path string) (string, error) {
)
}

if pSize == 0 {
tenValueDestroy(cValue)

// We can not allocate a []byte with size 0, so just return "".
return "", nil
}

return getPropStr(
uintptr(pSize),
func(v unsafe.Pointer) C.ten_go_status_t {
Expand Down Expand Up @@ -359,6 +366,13 @@ func (p *tenEnv) GetPropertyBytes(path string) ([]byte, error) {
)
}

if pSize == 0 {
tenValueDestroy(cValue)

// We can not allocate a []byte with size 0, so just return nil.
return nil, nil
}

return getPropBytes(
uintptr(pSize),
func(v unsafe.Pointer) C.ten_go_status_t {
Expand Down
Loading