Skip to content

Commit

Permalink
[nim] Prevent free_trusted_setup double call issue (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
jangko authored May 6, 2024
1 parent c9e4634 commit 98c419e
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bindings/nim/kzg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export

type
KzgCtx* = ref object
valFreed: bool
val: KzgSettings

KzgProofAndY* = object
Expand All @@ -32,7 +33,12 @@ else:
##############################################################

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 =
# Nim finalizer is still broken(v1.6)
Expand Down Expand Up @@ -209,8 +215,8 @@ 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 =
toCommitment(ctx, blob)
Expand Down

0 comments on commit 98c419e

Please sign in to comment.