Skip to content

Commit

Permalink
avoid double free
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 3, 2024
1 parent 5e1a79e commit a005c44
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion object.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (obj Object) object() Object {

func newObject(obj *PyObject) Object {
if obj == nil {
C.PyErr_Print()
return Object{}
}
o := &pyObject{obj: obj}
p := Object{o}
runtime.SetFinalizer(o, func(o *pyObject) {
// TODO: need better auto-release mechanism
// C.Py_DecRef(o.obj)
})
return p
Expand Down
4 changes: 4 additions & 0 deletions python.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func CompileString(code, filename string, start InputType) Object {
ccode := AllocCStr(code)
cfilename := AllocCStr(filename)
o := C.Py_CompileString(ccode, cfilename, C.int(start))
// TODO: check why double free
C.free(unsafe.Pointer(ccode))
C.free(unsafe.Pointer(cfilename))
return newObject(o)
Expand Down Expand Up @@ -82,6 +83,9 @@ func Nil() Object {
func RunString(code string) error {
// Get __main__ module dict for executing code
main := MainModule()
if main.Nil() {
return fmt.Errorf("failed to get __main__ module")
}

Check warning on line 88 in python.go

View check run for this annotation

Codecov / codecov/patch

python.go#L87-L88

Added lines #L87 - L88 were not covered by tests
dict := main.Dict()

// Run the code string
Expand Down

0 comments on commit a005c44

Please sign in to comment.