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

appsec/waf: fix memory release condition #1583

Merged
merged 2 commits into from
Nov 17, 2022
Merged
Changes from 1 commit
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
16 changes: 8 additions & 8 deletions internal/appsec/waf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,7 @@ func cFree(ptr unsafe.Pointer) {

// Exported Go function to free ddwaf objects by using freeWO in order to keep
// its dummy but efficient memory kallocation monitoring.
//
//export go_ddwaf_object_free
func go_ddwaf_object_free(v *C.ddwaf_object) {
freeWO((*wafObject)(v))
Expand All @@ -949,17 +950,16 @@ func go_ddwaf_object_free(v *C.ddwaf_object) {
// Go reimplementation of ddwaf_result_free to avoid yet another CGO call in the
// request hot-path and avoiding it when there are no results to free.
func freeWAFResult(result *C.ddwaf_result) {
if result.data == nil || result.actions.array == nil {
return
if data := result.data; data != nil {
C.free(unsafe.Pointer(data))
}

C.free(unsafe.Pointer(result.data))

array := result.actions.array
for i := 0; i < int(result.actions.size); i++ {
C.free(unsafe.Pointer(cindexCharPtrArray(array, i)))
if array := result.actions.array; array != nil {
for i := 0; i < int(result.actions.size); i++ {
C.free(unsafe.Pointer(cindexCharPtrArray(array, i)))
}
C.free(unsafe.Pointer(array))
}
C.free(unsafe.Pointer(array))
}

// Helper function to access to i-th element of the given **C.char array.
Expand Down