[nim] Prevent free_trusted_setup double call issue (#426)

This commit is contained in:
andri lim 2024-05-06 20:58:39 +07:00 committed by GitHub
parent c9e4634307
commit 98c419eed5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 9 additions and 3 deletions

View File

@ -13,6 +13,7 @@ export
type type
KzgCtx* = ref object KzgCtx* = ref object
valFreed: bool
val: KzgSettings val: KzgSettings
KzgProofAndY* = object KzgProofAndY* = object
@ -32,7 +33,12 @@ else:
############################################################## ##############################################################
proc destroy*(x: KzgCtx) = proc destroy*(x: KzgCtx) =
free_trusted_setup(x.val) # Prevent Nim GC to call free_trusted_setup
# if user already done it before.
# Otherwise, the program will crash with segfault.
if not x.valFreed:
free_trusted_setup(x.val)
x.valFreed = true
proc newKzgCtx(): KzgCtx = proc newKzgCtx(): KzgCtx =
# Nim finalizer is still broken(v1.6) # Nim finalizer is still broken(v1.6)
@ -209,8 +215,8 @@ template loadTrustedSetupFile*(input: File | string): untyped =
loadTrustedSetup(input) loadTrustedSetup(input)
template freeTrustedSetup*(ctx: KzgCtx) = template freeTrustedSetup*(ctx: KzgCtx) =
free_trusted_setup(ctx.val) destroy(ctx)
template blobToKzgCommitment*(ctx: KzgCtx, template blobToKzgCommitment*(ctx: KzgCtx,
blob: KzgBlob): untyped = blob: KzgBlob): untyped =
toCommitment(ctx, blob) toCommitment(ctx, blob)