From 2ab0c219fc7779fc4f10eee051456c0b8159bd4c Mon Sep 17 00:00:00 2001 From: Justin Traglia <95511699+jtraglia@users.noreply.github.com> Date: Mon, 4 Mar 2024 08:43:22 -0600 Subject: [PATCH] python: check file pointer when loading trusted setup (#401) Co-authored-by: George Kadianakis --- bindings/python/ckzg.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/bindings/python/ckzg.c b/bindings/python/ckzg.c index 0b8431a..94b998a 100644 --- a/bindings/python/ckzg.c +++ b/bindings/python/ckzg.c @@ -10,15 +10,24 @@ static void free_KZGSettings(PyObject *c) { static PyObject* load_trusted_setup_wrap(PyObject *self, PyObject *args) { PyObject *f; + FILE *fp; if (!PyArg_ParseTuple(args, "U", &f)) return PyErr_Format(PyExc_ValueError, "expected a string"); KZGSettings *s = (KZGSettings*)malloc(sizeof(KZGSettings)); - if (s == NULL) return PyErr_NoMemory(); - if (load_trusted_setup_file(s, fopen(PyUnicode_AsUTF8(f), "r")) != C_KZG_OK) { + fp = fopen(PyUnicode_AsUTF8(f), "r"); + if (fp == NULL) { + free(s); + return PyErr_Format(PyExc_RuntimeError, "error reading trusted setup"); + } + + C_KZG_RET ret = load_trusted_setup_file(s, fp); + fclose(fp); + + if (ret != C_KZG_OK) { free(s); return PyErr_Format(PyExc_RuntimeError, "error loading trusted setup"); }