Skip to content

Commit

Permalink
Merge pull request #40 from aashah/aashah/fix-fatal-runtime-error
Browse files Browse the repository at this point in the history
Fix fatal runtime panic passing invalid pointer
  • Loading branch information
flier authored Nov 23, 2021
2 parents 44d1508 + 7ae90e9 commit 287340c
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions hyperscan/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,8 @@ type hsMatchEventContext struct {

//export hsMatchEventCallback
func hsMatchEventCallback(id C.uint, from, to C.ulonglong, flags C.uint, data unsafe.Pointer) C.int {
ctx, ok := handle.Handle(data).Value().(hsMatchEventContext)
h := (*handle.Handle)(data)
ctx, ok := h.Value().(hsMatchEventContext)
if !ok {
return C.HS_INVALID
}
Expand Down Expand Up @@ -1043,7 +1044,7 @@ func hsScan(db hsDatabase, data []byte, flags ScanFlag, scratch hsScratch, onEve
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand Down Expand Up @@ -1087,7 +1088,7 @@ func hsScanVector(db hsDatabase, data [][]byte, flags ScanFlag, scratch hsScratc
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand Down Expand Up @@ -1127,7 +1128,7 @@ func hsScanStream(stream hsStream, data []byte, flags ScanFlag, scratch hsScratc
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

// Ensure go data is alive before the C function returns
runtime.KeepAlive(data)
Expand All @@ -1146,7 +1147,7 @@ func hsCloseStream(stream hsStream, scratch hsScratch, onEvent hsMatchEventHandl
ret := C.hs_close_stream(stream,
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand All @@ -1163,7 +1164,7 @@ func hsResetStream(stream hsStream, flags ScanFlag, scratch hsScratch, onEvent h
C.uint(flags),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand All @@ -1190,7 +1191,7 @@ func hsResetAndCopyStream(to, from hsStream, scratch hsScratch, onEvent hsMatchE
from,
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

if ret != C.HS_SUCCESS {
return HsError(ret)
Expand Down Expand Up @@ -1238,7 +1239,7 @@ func hsResetAndExpandStream(stream hsStream, buf []byte, scratch hsScratch, onEv
C.size_t(len(buf)),
scratch,
C.match_event_handler(C.hsMatchEventCallback),
unsafe.Pointer(h))
unsafe.Pointer(&h))

runtime.KeepAlive(buf)

Expand Down

0 comments on commit 287340c

Please sign in to comment.