Skip to content

Possible memory leak: wrapError passes C.CString(message) to wrap_result_error and never frees it #54

Open

Description

@OvOhao

Description

In pkg/janet/interop.go:107-109 (commit a08b5cb):

func wrapError(message string) C.Janet {
	return C.wrap_result_error(C.CString(message))
}

wrap_result_error (pkg/janet/api.c:14-21) builds janet_wrap_string(janet_cstring(message)), which copies the bytes into a Janet string, so the C.CString buffer is not referenced after the call. It is never freed.

wrapError is called from poll (module.go:128-131) whenever a Go callback registered through VM.Callback / VM.Module returns a non-nil error. Each such error leaves len(err.Error())+1 bytes allocated in the running server.

Reproduction

A small program using this package (macOS arm64, Go 1.27) registers a callback that always returns a 100-byte error and calls it N times through ExecuteCall. Counting live blocks with heap on the paused process, the 112-byte bucket grew by 905 for 900 additional calls and by 924 for 1000 more (one block per failing call). With a callback that returns nil it stayed flat. With the fix below, leaks --atExit at N=1000 reports no 112-byte blocks.

Suggested fix

func wrapError(message string) C.Janet {
	cMessage := C.CString(message)
	defer C.free(unsafe.Pointer(cMessage))
	return C.wrap_result_error(cMessage)
}

If you could credit me as a reporter for my contributions to security advisory I will be thankful.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions