From 98c419eed5fb3bd4d293be4a56fed71c53d1bc99 Mon Sep 17 00:00:00 2001 From: andri lim Date: Mon, 6 May 2024 20:58:39 +0700 Subject: [PATCH] [nim] Prevent free_trusted_setup double call issue (#426) --- bindings/nim/kzg.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/bindings/nim/kzg.nim b/bindings/nim/kzg.nim index 41709d9..eb892b6 100644 --- a/bindings/nim/kzg.nim +++ b/bindings/nim/kzg.nim @@ -13,6 +13,7 @@ export type KzgCtx* = ref object + valFreed: bool val: KzgSettings KzgProofAndY* = object @@ -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) @@ -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)