Skip to content

Commit 6d107c6

Browse files
committed
fix: also malloc user data pointer in memory iterator
1 parent 9471172 commit 6d107c6

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

mem_blocks.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,12 @@ func makeMemoryBlockIteratorContainer(mbi MemoryBlockIterator) (c *memoryBlockIt
6262
return
6363
}
6464

65-
// The caller is responsible to delete the cgoHandle cmbi.context.
65+
// The caller is responsible to delete the cgoHandle *cmbi.context and free cmbi.context.
6666
func makeCMemoryBlockIterator(c *memoryBlockIteratorContainer) (cmbi *C.YR_MEMORY_BLOCK_ITERATOR) {
67+
userData := (*cgoHandle)(C.malloc(C.size_t(unsafe.Sizeof(cgoHandle(0)))))
68+
*userData = cgoNewHandle(c)
6769
cmbi = &C.YR_MEMORY_BLOCK_ITERATOR{
68-
context: unsafe.Pointer(cgoNewHandle(c)),
70+
context: unsafe.Pointer(userData),
6971
first: C.YR_MEMORY_BLOCK_ITERATOR_FUNC(C.memoryBlockIteratorFirst),
7072
next: C.YR_MEMORY_BLOCK_ITERATOR_FUNC(C.memoryBlockIteratorNext),
7173
}
@@ -108,7 +110,7 @@ type MemoryBlock struct {
108110
//
109111
//export memoryBlockFetch
110112
func memoryBlockFetch(cblock *C.YR_MEMORY_BLOCK) *C.uint8_t {
111-
c := cgoHandle(cblock.context).Value().(*memoryBlockIteratorContainer)
113+
c := ((*cgoHandle)(cblock.context)).Value().(*memoryBlockIteratorContainer)
112114
c.realloc(int(cblock.size))
113115
c.MemoryBlock.FetchData(c.buf)
114116
return (*C.uint8_t)(unsafe.Pointer(&c.buf[0]))
@@ -143,7 +145,7 @@ func memoryBlockIteratorCommon(cmbi *C.YR_MEMORY_BLOCK_ITERATOR, c *memoryBlockI
143145
//
144146
//export memoryBlockIteratorFirst
145147
func memoryBlockIteratorFirst(cmbi *C.YR_MEMORY_BLOCK_ITERATOR) *C.YR_MEMORY_BLOCK {
146-
c := cgoHandle(cmbi.context).Value().(*memoryBlockIteratorContainer)
148+
c := ((*cgoHandle)(cmbi.context)).Value().(*memoryBlockIteratorContainer)
147149
c.MemoryBlock = c.MemoryBlockIterator.First()
148150
return memoryBlockIteratorCommon(cmbi, c)
149151
}
@@ -153,13 +155,13 @@ func memoryBlockIteratorFirst(cmbi *C.YR_MEMORY_BLOCK_ITERATOR) *C.YR_MEMORY_BLO
153155
//
154156
//export memoryBlockIteratorNext
155157
func memoryBlockIteratorNext(cmbi *C.YR_MEMORY_BLOCK_ITERATOR) *C.YR_MEMORY_BLOCK {
156-
c := cgoHandle(cmbi.context).Value().(*memoryBlockIteratorContainer)
158+
c := ((*cgoHandle)(cmbi.context)).Value().(*memoryBlockIteratorContainer)
157159
c.MemoryBlock = c.MemoryBlockIterator.Next()
158160
return memoryBlockIteratorCommon(cmbi, c)
159161
}
160162

161163
//export memoryBlockIteratorFilesize
162164
func memoryBlockIteratorFilesize(cmbi *C.YR_MEMORY_BLOCK_ITERATOR) C.uint64_t {
163-
c := cgoHandle(cmbi.context).Value().(*memoryBlockIteratorContainer)
165+
c := ((*cgoHandle)(cmbi.context)).Value().(*memoryBlockIteratorContainer)
164166
return C.uint64_t(c.MemoryBlockIterator.(MemoryBlockIteratorWithFilesize).Filesize())
165167
}

rules.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ func (r *Rules) ScanMemBlocks(mbi MemoryBlockIterator, flags ScanFlags, timeout
152152
c := makeMemoryBlockIteratorContainer(mbi)
153153
defer c.free()
154154
cmbi := makeCMemoryBlockIterator(c)
155-
defer cgoHandle(cmbi.context).Delete()
155+
defer C.free(cmbi.context)
156+
defer ((*cgoHandle)(cmbi.context)).Delete()
156157
userData := cgoNewHandle(makeScanCallbackContainer(cb, r))
157158
defer userData.Delete()
158159
err = newError(C.yr_rules_scan_mem_blocks(

scanner.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ func (s *Scanner) ScanMemBlocks(mbi MemoryBlockIterator) (err error) {
214214
c := makeMemoryBlockIteratorContainer(mbi)
215215
defer c.free()
216216
cmbi := makeCMemoryBlockIterator(c)
217-
defer cgoHandle(cmbi.context).Delete()
217+
defer C.free(cmbi.context)
218+
defer ((*cgoHandle)(cmbi.context)).Delete()
218219
s.putCallbackData()
219220
C.yr_scanner_set_flags(s.cptr, s.flags.withReportFlags(s.Callback))
220221
err = newError(C.yr_scanner_scan_mem_blocks(

0 commit comments

Comments
 (0)