From 6ece83760d3501ee34a8ee2076751edb8945dd81 Mon Sep 17 00:00:00 2001 From: jsteube Date: Sat, 24 Dec 2016 00:40:40 +0100 Subject: [PATCH] Add wrapping function for asprintf() --- include/shared.h | 3 +++ src/dictstat.c | 3 ++- src/folder.c | 12 ++++++------ src/hwmon.c | 2 +- src/induct.c | 4 ++-- src/logfile.c | 3 ++- src/loopback.c | 2 +- src/outfile_check.c | 2 +- src/potfile.c | 2 +- src/restore.c | 8 ++++---- src/shared.c | 9 +++++++++ src/tuningdb.c | 3 ++- 12 files changed, 34 insertions(+), 19 deletions(-) diff --git a/include/shared.h b/include/shared.h index f5e6a3cd0..c7e884cb4 100644 --- a/include/shared.h +++ b/include/shared.h @@ -6,6 +6,7 @@ #ifndef _SHARED_H #define _SHARED_H +#include #include #include #include @@ -24,6 +25,8 @@ char *filename_from_filepath (char *filepath); void naive_replace (char *s, const char key_char, const char replace_char); void naive_escape (char *s, size_t s_max, const char key_char, const char escape_char); +void hc_asprintf (char **strp, const char *fmt, ...); + void hc_sleep_msec (const u32 msec); void hc_sleep (const u32 sec); diff --git a/src/dictstat.c b/src/dictstat.c index d49471810..a39bc74aa 100644 --- a/src/dictstat.c +++ b/src/dictstat.c @@ -9,6 +9,7 @@ #include "event.h" #include "dictstat.h" #include "locking.h" +#include "shared.h" int sort_by_dictstat (const void *s1, const void *s2) { @@ -46,7 +47,7 @@ int dictstat_init (hashcat_ctx_t *hashcat_ctx) dictstat_ctx->base = (dictstat_t *) hccalloc (MAX_DICTSTAT, sizeof (dictstat_t)); dictstat_ctx->cnt = 0; - asprintf (&dictstat_ctx->filename, "%s/hashcat.dictstat", folder_config->profile_dir); + hc_asprintf (&dictstat_ctx->filename, "%s/hashcat.dictstat", folder_config->profile_dir); FILE *fp = fopen (dictstat_ctx->filename, "ab"); diff --git a/src/folder.c b/src/folder.c index 526816c6d..1c2c62ab8 100644 --- a/src/folder.c +++ b/src/folder.c @@ -28,7 +28,7 @@ static int get_exec_path (char *exec_path, const size_t exec_path_sz) char *tmp; - asprintf (&tmp, "/proc/%d/exe", getpid ()); + hc_asprintf (&tmp, "/proc/%d/exe", getpid ()); const ssize_t len = readlink (tmp, exec_path, exec_path_sz - 1); @@ -221,7 +221,7 @@ char **scan_directory (const char *path) char *path_file; - asprintf (&path_file, "%s/%s", tmp_path, de->d_name); + hc_asprintf (&path_file, "%s/%s", tmp_path, de->d_name); DIR *d_test; @@ -393,7 +393,7 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins #if defined (_WIN) - asprintf (&cpath, "%s\\OpenCL\\", shared_dir); + hc_asprintf (&cpath, "%s\\OpenCL\\", shared_dir); char *cpath_real = (char *) hcmalloc (HCBUFSIZ_TINY); @@ -406,7 +406,7 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins #else - asprintf (&cpath, "%s/OpenCL/", shared_dir); + hc_asprintf (&cpath, "%s/OpenCL/", shared_dir); char *cpath_real = (char *) hcmalloc (PATH_MAX); @@ -426,7 +426,7 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins { char *tmp; - asprintf (&tmp, "TMP=%s", cpath_real); + hc_asprintf (&tmp, "TMP=%s", cpath_real); putenv (tmp); } @@ -450,7 +450,7 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins char *kernels_folder; - asprintf (&kernels_folder, "%s/kernels", profile_dir); + hc_asprintf (&kernels_folder, "%s/kernels", profile_dir); hc_mkdir (kernels_folder, 0700); diff --git a/src/hwmon.c b/src/hwmon.c index d66c62a75..22fdc587c 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -57,7 +57,7 @@ static char *hm_SYSFS_get_syspath_device (hashcat_ctx_t *hashcat_ctx, const int char *syspath; - asprintf (&syspath, "%s/0000:%02x:%02x.%01x", SYS_BUS_PCI_DEVICES, device_param->pcie_bus, device_param->pcie_device, device_param->pcie_function); + hc_asprintf (&syspath, "%s/0000:%02x:%02x.%01x", SYS_BUS_PCI_DEVICES, device_param->pcie_bus, device_param->pcie_device, device_param->pcie_function); return syspath; } diff --git a/src/induct.c b/src/induct.c index 09466a081..4fb4d1f28 100644 --- a/src/induct.c +++ b/src/induct.c @@ -50,7 +50,7 @@ int induct_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *root_directory; - asprintf (&root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR); + hc_asprintf (&root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR); if (rmdir (root_directory) == -1) { @@ -62,7 +62,7 @@ int induct_ctx_init (hashcat_ctx_t *hashcat_ctx) { char *root_directory_mv; - asprintf (&root_directory_mv, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) time (NULL)); + hc_asprintf (&root_directory_mv, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) time (NULL)); if (rename (root_directory, root_directory_mv) != 0) { diff --git a/src/logfile.c b/src/logfile.c index 89b5bc001..25c73f4f0 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -9,6 +9,7 @@ #include "event.h" #include "logfile.h" #include "locking.h" +#include "shared.h" static int logfile_generate_id (void) { @@ -83,7 +84,7 @@ int logfile_init (hashcat_ctx_t *hashcat_ctx) if (user_options->logfile_disable == true) return 0; - asprintf (&logfile_ctx->logfile, "%s/%s.log", folder_config->session_dir, user_options->session); + hc_asprintf (&logfile_ctx->logfile, "%s/%s.log", folder_config->session_dir, user_options->session); logfile_ctx->subid = (char *) hcmalloc (HCBUFSIZ_TINY); logfile_ctx->topid = (char *) hcmalloc (HCBUFSIZ_TINY); diff --git a/src/loopback.c b/src/loopback.c index c3e104c73..3068c9fb0 100644 --- a/src/loopback.c +++ b/src/loopback.c @@ -102,7 +102,7 @@ int loopback_write_open (hashcat_ctx_t *hashcat_ctx) const u32 random_num = get_random_num (0, 9999); - asprintf (&loopback_ctx->filename, "%s/%s.%d_%u", induct_ctx->root_directory, LOOPBACK_FILE, (int) now, random_num); + hc_asprintf (&loopback_ctx->filename, "%s/%s.%d_%u", induct_ctx->root_directory, LOOPBACK_FILE, (int) now, random_num); FILE *fp = fopen (loopback_ctx->filename, "ab"); diff --git a/src/outfile_check.c b/src/outfile_check.c index de21547a1..31583ccbc 100644 --- a/src/outfile_check.c +++ b/src/outfile_check.c @@ -339,7 +339,7 @@ int outcheck_ctx_init (hashcat_ctx_t *hashcat_ctx) if (user_options->outfile_check_dir == NULL) { - asprintf (&outcheck_ctx->root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, OUTFILES_DIR); + hc_asprintf (&outcheck_ctx->root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, OUTFILES_DIR); } else { diff --git a/src/potfile.c b/src/potfile.c index bbd114ca2..aac22d6f0 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -99,7 +99,7 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx) { potfile_ctx->fp = NULL; - asprintf (&potfile_ctx->filename, "%s/hashcat.potfile", folder_config->profile_dir); + hc_asprintf (&potfile_ctx->filename, "%s/hashcat.potfile", folder_config->profile_dir); } else { diff --git a/src/restore.c b/src/restore.c index c9cf62713..a9a0d95f0 100644 --- a/src/restore.c +++ b/src/restore.c @@ -49,7 +49,7 @@ static int check_running_process (hashcat_ctx_t *hashcat_ctx) char *pidbin; - asprintf (&pidbin, "/proc/%u/cmdline", rd->pid); + hc_asprintf (&pidbin, "/proc/%u/cmdline", rd->pid); FILE *fd = fopen (pidbin, "rb"); @@ -356,13 +356,13 @@ int restore_ctx_init (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) if (user_options->restore_file_path == NULL) { - asprintf (&restore_ctx->eff_restore_file, "%s/%s.restore", folder_config->session_dir, user_options->session); - asprintf (&restore_ctx->new_restore_file, "%s/%s.restore.new", folder_config->session_dir, user_options->session); + hc_asprintf (&restore_ctx->eff_restore_file, "%s/%s.restore", folder_config->session_dir, user_options->session); + hc_asprintf (&restore_ctx->new_restore_file, "%s/%s.restore.new", folder_config->session_dir, user_options->session); } else { restore_ctx->eff_restore_file = hcstrdup (user_options->restore_file_path); - asprintf (&restore_ctx->new_restore_file, "%s.new", user_options->restore_file_path); + hc_asprintf (&restore_ctx->new_restore_file, "%s.new", user_options->restore_file_path); } restore_ctx->argc = argc; diff --git a/src/shared.c b/src/shared.c index 9c8678e39..3777bb9b5 100644 --- a/src/shared.c +++ b/src/shared.c @@ -99,6 +99,15 @@ void naive_escape (char *s, size_t s_max, const char key_char, const char escape strncpy (s, s_escaped, s_max - 1); } +void hc_asprintf (char **strp, const char *fmt, ...) +{ + va_list args; + va_start (args, fmt); + int rc __attribute__((unused)); + rc = asprintf (strp, fmt, args); + va_end (args); +} + #if defined (_POSIX) int hc_stat (const char *pathname, hc_stat_t *buf) { diff --git a/src/tuningdb.c b/src/tuningdb.c index b809e2503..2e924f96b 100644 --- a/src/tuningdb.c +++ b/src/tuningdb.c @@ -15,6 +15,7 @@ #include "thread.h" #include "opencl.h" #include "hashes.h" +#include "shared.h" static int sort_by_tuning_db_alias (const void *v1, const void *v2) { @@ -69,7 +70,7 @@ int tuning_db_init (hashcat_ctx_t *hashcat_ctx) char *tuning_db_file; - asprintf (&tuning_db_file, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); + hc_asprintf (&tuning_db_file, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); FILE *fp = fopen (tuning_db_file, "rb");