using cstring instead of nim strings to avoid segfault (#3108)

This commit is contained in:
gabrielmer 2024-10-11 13:57:55 +03:00 committed by GitHub
parent 37edaf6264
commit 78759f6e52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 33 deletions

View File

@ -62,13 +62,13 @@ proc handleRes[T: string | void](
foreignThreadGc: foreignThreadGc:
let msg = "libwaku error: " & $res.error let msg = "libwaku error: " & $res.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR return RET_ERR
when T is string: foreignThreadGc:
let msg = $res.get() var msg: cstring = ""
if msg.len > 0: when T is string:
foreignThreadGc: msg = res.get().cstring()
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData) callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK return RET_OK
proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler = proc relayEventCallback(ctx: ptr WakuContext): WakuRelayHandler =
@ -487,38 +487,26 @@ proc waku_get_peerids_from_peerstore(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let connRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.PEER_MANAGER, RequestType.PEER_MANAGER,
PeerManagementRequest.createShared(PeerManagementMsgType.GET_ALL_PEER_IDS), PeerManagementRequest.createShared(PeerManagementMsgType.GET_ALL_PEER_IDS),
) )
if connRes.isErr(): .handleRes(callback, userData)
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_get_peerids_by_protocol( proc waku_get_peerids_by_protocol(
ctx: ptr WakuContext, protocol: cstring, callback: WakuCallBack, userData: pointer ctx: ptr WakuContext, protocol: cstring, callback: WakuCallBack, userData: pointer
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let connRes = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, ctx,
RequestType.PEER_MANAGER, RequestType.PEER_MANAGER,
PeerManagementRequest.createGetPeerIdsByProtocolRequest($protocol), PeerManagementRequest.createGetPeerIdsByProtocolRequest($protocol),
) )
if connRes.isErr(): .handleRes(callback, userData)
let msg = $connRes.error
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $connRes.value
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
proc waku_store_query( proc waku_store_query(
ctx: ptr WakuContext, ctx: ptr WakuContext,
@ -628,16 +616,11 @@ proc waku_peer_exchange_request(
): cint {.dynlib, exportc.} = ): cint {.dynlib, exportc.} =
checkLibwakuParams(ctx, callback, userData) checkLibwakuParams(ctx, callback, userData)
let discoveredPeers = waku_thread.sendRequestToWakuThread( waku_thread
.sendRequestToWakuThread(
ctx, RequestType.DISCOVERY, DiscoveryRequest.createPeerExchangeRequest(numPeers) ctx, RequestType.DISCOVERY, DiscoveryRequest.createPeerExchangeRequest(numPeers)
).valueOr: )
let msg = $error .handleRes(callback, userData)
callback(RET_ERR, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_ERR
let msg = $discoveredPeers
callback(RET_OK, unsafeAddr msg[0], cast[csize_t](len(msg)), userData)
return RET_OK
### End of exported procs ### End of exported procs
################################################################################ ################################################################################