[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
KzgCtx* = ref object
valFreed: bool
val: KzgSettings
KzgProofAndY* = object
@ -32,7 +33,12 @@ else:
##############################################################
proc destroy*(x: KzgCtx) =
# 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 =
# Nim finalizer is still broken(v1.6)
@ -209,7 +215,7 @@ template loadTrustedSetupFile*(input: File | string): untyped =
loadTrustedSetup(input)
template freeTrustedSetup*(ctx: KzgCtx) =
free_trusted_setup(ctx.val)
destroy(ctx)
template blobToKzgCommitment*(ctx: KzgCtx,
blob: KzgBlob): untyped =