From 6bf5ee95a642f5585bf2581928134e438d028b35 Mon Sep 17 00:00:00 2001 From: Gabriele Gristina Date: Sun, 30 Oct 2016 16:08:41 +0100 Subject: [PATCH] Fix resource leak --- src/dispatch.c | 7 ++++++- src/mpsp.c | 11 ++++++++++- src/opencl.c | 10 +++++----- src/restore.c | 10 ++++++++-- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/dispatch.c b/src/dispatch.c index 206ae6481..1fb7a22c4 100644 --- a/src/dispatch.c +++ b/src/dispatch.c @@ -441,7 +441,12 @@ static int calc (hashcat_ctx_t *hashcat_ctx, hc_device_param_t *device_param) const int rc_wl_data_init = wl_data_init (hashcat_ctx_tmp); - if (rc_wl_data_init == -1) return -1; + if (rc_wl_data_init == -1) + { + fclose (fd); + + return -1; + } u64 words_cur = 0; diff --git a/src/mpsp.c b/src/mpsp.c index 231cc90df..08b41222f 100644 --- a/src/mpsp.c +++ b/src/mpsp.c @@ -524,7 +524,12 @@ static int mp_setup_usr (hashcat_ctx_t *hashcat_ctx, cs_t *mp_sys, cs_t *mp_usr, { const int rc = mp_expand (hashcat_ctx, buf, strlen (buf), mp_sys, mp_usr, index, 1); - if (rc == -1) return -1; + if (rc == -1) + { + if (fp) fclose (fp); + + return -1; + } } else { @@ -534,6 +539,8 @@ static int mp_setup_usr (hashcat_ctx_t *hashcat_ctx, cs_t *mp_sys, cs_t *mp_usr, fclose (fp); + fp = NULL; + len = in_superchop (mp_file); if (len == 0) @@ -552,6 +559,8 @@ static int mp_setup_usr (hashcat_ctx_t *hashcat_ctx, cs_t *mp_sys, cs_t *mp_usr, } } + if (fp) fclose (fp); + return 0; } diff --git a/src/opencl.c b/src/opencl.c index 828d5a328..79bc2876a 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -227,6 +227,8 @@ static int read_kernel_binary (hashcat_ctx_t *hashcat_ctx, const char *kernel_fi size_t num_read = fread (buf, sizeof (char), st.st_size, fp); + fclose (fp); + if (num_read != (size_t) st.st_size) { event_log_error (hashcat_ctx, "%s: %s", kernel_file, strerror (errno)); @@ -234,8 +236,6 @@ static int read_kernel_binary (hashcat_ctx_t *hashcat_ctx, const char *kernel_fi return -1; } - fclose (fp); - buf[st.st_size] = 0; for (int i = 0; i < num_devices; i++) @@ -3428,18 +3428,18 @@ int opencl_session_begin (hashcat_ctx_t *hashcat_ctx) return -1; } - char buf[1]; + char buf[1] = { 0 }; size_t n = fread (buf, 1, 1, fd); + fclose (fd); + if (n != 1) { event_log_error (hashcat_ctx, "%s: %s", files_names[i], strerror (errno)); return -1; } - - fclose (fd); } // we don't have sm_* on vendors not NV but it doesn't matter diff --git a/src/restore.c b/src/restore.c index 9145e1b1c..ac681604f 100644 --- a/src/restore.c +++ b/src/restore.c @@ -33,6 +33,8 @@ static int check_running_process (hashcat_ctx_t *hashcat_ctx) const size_t nread = fread (rd, sizeof (restore_data_t), 1, fp); + fclose (fp); + if (nread != 1) { event_log_error (hashcat_ctx, "Cannot read %s", eff_restore_file); @@ -40,8 +42,6 @@ static int check_running_process (hashcat_ctx_t *hashcat_ctx) return -1; } - fclose (fp); - if (rd->pid) { char *pidbin = (char *) hcmalloc (hashcat_ctx, HCBUFSIZ_LARGE); VERIFY_PTR (pidbin); @@ -175,6 +175,8 @@ static int read_restore (hashcat_ctx_t *hashcat_ctx) { event_log_error (hashcat_ctx, "Can't read %s", eff_restore_file); + fclose (fp); + return -1; } @@ -188,6 +190,8 @@ static int read_restore (hashcat_ctx_t *hashcat_ctx) { event_log_error (hashcat_ctx, "Can't read %s", eff_restore_file); + fclose (fp); + return -1; } @@ -248,6 +252,8 @@ static int write_restore (hashcat_ctx_t *hashcat_ctx) { event_log_error (hashcat_ctx, "setvbuf file '%s': %s", new_restore_file, strerror (errno)); + fclose (fp); + return -1; }