From b90abcc3074286ef9e3bc879438e37e558033da2 Mon Sep 17 00:00:00 2001 From: q-posev Date: Mon, 13 Dec 2021 09:59:27 +0100 Subject: [PATCH] fix memory leak in the Fortran test --- src/templates_front/templator_front.org | 71 ++++++++++++------------- tests/test_f.f90 | 3 ++ 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index 4bbb6b0..dab7796 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -880,42 +880,6 @@ trexio_open(const char* file_name, const char mode, return NULL; } - rc = trexio_has_metadata_package_version(result); - if (rc == TREXIO_FAILURE) { - if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR; - free(result); - return NULL; - } - - if (rc == TREXIO_HAS_NOT) { - switch (back_end) { - - case TREXIO_TEXT: - rc = trexio_text_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION); - break; - - case TREXIO_HDF5: -#ifdef HAVE_HDF5 - rc = trexio_hdf5_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION); - break; -#else - if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING; - return NULL; -#endif -/* - case TREXIO_JSON: - rc = trexio_json_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION); - break; -,*/ - } - } - - if (rc != TREXIO_SUCCESS) { - if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR; - free(result); - return NULL; - } - /* File locking */ @@ -948,7 +912,42 @@ trexio_open(const char* file_name, const char mode, return NULL; } + /* Write metadata (i.e. package version) upon creation */ + rc = trexio_has_metadata_package_version(result); + if (rc == TREXIO_FAILURE) { + if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR; + free(result); + return NULL; + } + + if (rc == TREXIO_HAS_NOT) { + switch (back_end) { + + case TREXIO_TEXT: + rc = trexio_text_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION); + break; + + case TREXIO_HDF5: +#ifdef HAVE_HDF5 + rc = trexio_hdf5_write_metadata_package_version(result, TREXIO_PACKAGE_VERSION); + break; +#else + if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING; + return NULL; +#endif + + } + } + + if (rc != TREXIO_SUCCESS) { + if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR; + free(result); + return NULL; + } + + /* Exit upon success */ if (rc_open != NULL) *rc_open = TREXIO_SUCCESS; + return result; } #+end_src diff --git a/tests/test_f.f90 b/tests/test_f.f90 index 006f4de..0d1fe9e 100644 --- a/tests/test_f.f90 +++ b/tests/test_f.f90 @@ -338,6 +338,9 @@ subroutine test_read_void(file_name, back_end) ! ================= START OF TEST ===================== ! trex_file = trexio_open(file_name, 'r', back_end, rc) + if (rc /= TREXIO_OPEN_ERROR) then + rc = trexio_close(trex_file) + endif call trexio_assert(rc, TREXIO_OPEN_ERROR) call trexio_string_of_error(rc, str)