From 36bd1c7dd9a58b873cfdec1aa94db6567a5f2b3a Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Mon, 19 Dec 2016 16:46:30 -0800 Subject: [PATCH] Replace some snprintf calls with asprintf. Simplifies the code. --- src/dictstat.c | 3 +-- src/folder.c | 26 +++++++++++++------------- src/hwmon.c | 4 ++-- src/induct.c | 10 ++++++---- src/logfile.c | 4 +--- src/loopback.c | 2 +- src/outfile_check.c | 4 +--- src/potfile.c | 3 +-- src/restore.c | 15 +++++---------- src/tuningdb.c | 4 ++-- src/wordlist.c | 6 ++---- 11 files changed, 35 insertions(+), 46 deletions(-) diff --git a/src/dictstat.c b/src/dictstat.c index c1c5b4f6a..d49471810 100644 --- a/src/dictstat.c +++ b/src/dictstat.c @@ -43,11 +43,10 @@ int dictstat_init (hashcat_ctx_t *hashcat_ctx) if (user_options->attack_mode == ATTACK_MODE_BF) return 0; dictstat_ctx->enabled = true; - dictstat_ctx->filename = (char *) hcmalloc (HCBUFSIZ_TINY); dictstat_ctx->base = (dictstat_t *) hccalloc (MAX_DICTSTAT, sizeof (dictstat_t)); dictstat_ctx->cnt = 0; - snprintf (dictstat_ctx->filename, HCBUFSIZ_TINY - 1, "%s/hashcat.dictstat", folder_config->profile_dir); + 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 c45846d24..526816c6d 100644 --- a/src/folder.c +++ b/src/folder.c @@ -26,12 +26,14 @@ static int get_exec_path (char *exec_path, const size_t exec_path_sz) { #if defined (__linux__) || defined (__CYGWIN__) - char tmp[32] = { 0 }; + char *tmp; - snprintf (tmp, sizeof (tmp) - 1, "/proc/%d/exe", getpid ()); + asprintf (&tmp, "/proc/%d/exe", getpid ()); const ssize_t len = readlink (tmp, exec_path, exec_path_sz - 1); + hcfree (tmp); + if (len == -1) return -1; #elif defined (_WIN) @@ -59,8 +61,6 @@ static int get_exec_path (char *exec_path, const size_t exec_path_sz) mib[2] = KERN_PROC_PATHNAME; mib[3] = -1; - char tmp[32] = { 0 }; - size_t size = exec_path_sz; sysctl (mib, 4, exec_path, &size, NULL, 0); @@ -219,9 +219,9 @@ char **scan_directory (const char *path) if ((strncmp (de->d_name, ".", strlen (de->d_name)) == 0) || (strncmp (de->d_name, "..", strlen (de->d_name)) == 0)) continue; - char *path_file = (char *) hcmalloc (HCBUFSIZ_TINY); + char *path_file; - snprintf (path_file, HCBUFSIZ_TINY - 1, "%s/%s", tmp_path, de->d_name); + asprintf (&path_file, "%s/%s", tmp_path, de->d_name); DIR *d_test; @@ -389,11 +389,11 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins * The best workaround found so far is to modify the TMP variable (only inside hashcat process) before the runtime is load */ - char *cpath = (char *) hcmalloc (HCBUFSIZ_TINY); + char *cpath; #if defined (_WIN) - snprintf (cpath, HCBUFSIZ_TINY - 1, "%s\\OpenCL\\", shared_dir); + 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 - snprintf (cpath, HCBUFSIZ_TINY - 1, "%s/OpenCL/", shared_dir); + asprintf (&cpath, "%s/OpenCL/", shared_dir); char *cpath_real = (char *) hcmalloc (PATH_MAX); @@ -424,9 +424,9 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins //if (getenv ("TMP") == NULL) if (1) { - char tmp[1000]; + char *tmp; - snprintf (tmp, sizeof (tmp) - 1, "TMP=%s", cpath_real); + asprintf (&tmp, "TMP=%s", cpath_real); putenv (tmp); } @@ -448,9 +448,9 @@ int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *ins * kernel cache, we need to make sure folder exist */ - char *kernels_folder = (char *) hcmalloc (HCBUFSIZ_TINY); + char *kernels_folder; - snprintf (kernels_folder, HCBUFSIZ_TINY - 1, "%s/kernels", profile_dir); + asprintf (&kernels_folder, "%s/kernels", profile_dir); hc_mkdir (kernels_folder, 0700); diff --git a/src/hwmon.c b/src/hwmon.c index b6d37ced5..d66c62a75 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -55,9 +55,9 @@ static char *hm_SYSFS_get_syspath_device (hashcat_ctx_t *hashcat_ctx, const int hc_device_param_t *device_param = &opencl_ctx->devices_param[device_id]; - char *syspath = hcmalloc (HCBUFSIZ_TINY); + char *syspath; - snprintf (syspath, HCBUFSIZ_TINY - 1, "%s/0000:%02x:%02x.%01x", SYS_BUS_PCI_DEVICES, device_param->pcie_bus, device_param->pcie_device, device_param->pcie_function); + 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 32babb4ec..1c15a1fb5 100644 --- a/src/induct.c +++ b/src/induct.c @@ -47,9 +47,9 @@ int induct_ctx_init (hashcat_ctx_t *hashcat_ctx) if (user_options->induction_dir == NULL) { - char *root_directory = (char *) hcmalloc (HCBUFSIZ_TINY); + char *root_directory; - snprintf (root_directory, HCBUFSIZ_TINY - 1, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR); + asprintf (&root_directory, "%s/%s.%s", folder_config->session_dir, user_options->session, INDUCT_DIR); if (rmdir (root_directory) == -1) { @@ -59,9 +59,9 @@ int induct_ctx_init (hashcat_ctx_t *hashcat_ctx) } else if (errno == ENOTEMPTY) { - char *root_directory_mv = (char *) hcmalloc (HCBUFSIZ_TINY); + char *root_directory_mv; - snprintf (root_directory_mv, HCBUFSIZ_TINY - 1, "%s/%s.induct.%d", folder_config->session_dir, user_options->session, (int) time (NULL)); + 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) { @@ -69,6 +69,8 @@ int induct_ctx_init (hashcat_ctx_t *hashcat_ctx) return -1; } + + hcfree (root_directory_mv); } else { diff --git a/src/logfile.c b/src/logfile.c index 58644ee2c..89b5bc001 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -83,9 +83,7 @@ int logfile_init (hashcat_ctx_t *hashcat_ctx) if (user_options->logfile_disable == true) return 0; - logfile_ctx->logfile = (char *) hcmalloc (HCBUFSIZ_TINY); - - snprintf (logfile_ctx->logfile, HCBUFSIZ_TINY - 1, "%s/%s.log", folder_config->session_dir, user_options->session); + 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 6467fc2fe..a112e6f6a 100644 --- a/src/loopback.c +++ b/src/loopback.c @@ -101,7 +101,7 @@ int loopback_write_open (hashcat_ctx_t *hashcat_ctx) const u32 random_num = get_random_num (0, 9999); - snprintf (loopback_ctx->filename, HCBUFSIZ_TINY - 1, "%s/%s.%d_%u", induct_ctx->root_directory, LOOPBACK_FILE, (int) now, random_num); + 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 f06f71a75..a1e9d5fbd 100644 --- a/src/outfile_check.c +++ b/src/outfile_check.c @@ -338,9 +338,7 @@ int outcheck_ctx_init (hashcat_ctx_t *hashcat_ctx) if (user_options->outfile_check_dir == NULL) { - outcheck_ctx->root_directory = (char *) hcmalloc (HCBUFSIZ_TINY); - - snprintf (outcheck_ctx->root_directory, HCBUFSIZ_TINY - 1, "%s/%s.%s", folder_config->session_dir, user_options->session, OUTFILES_DIR); + 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 57b78f1a8..35b0060dc 100644 --- a/src/potfile.c +++ b/src/potfile.c @@ -96,10 +96,9 @@ int potfile_init (hashcat_ctx_t *hashcat_ctx) if (user_options->potfile_path == NULL) { - potfile_ctx->filename = (char *) hcmalloc (HCBUFSIZ_TINY); potfile_ctx->fp = NULL; - snprintf (potfile_ctx->filename, HCBUFSIZ_TINY - 1, "%s/hashcat.potfile", folder_config->profile_dir); + asprintf (&potfile_ctx->filename, "%s/hashcat.potfile", folder_config->profile_dir); } else { diff --git a/src/restore.c b/src/restore.c index e40c40b86..b6de9e40c 100644 --- a/src/restore.c +++ b/src/restore.c @@ -47,9 +47,9 @@ static int check_running_process (hashcat_ctx_t *hashcat_ctx) { #if defined (_POSIX) - char *pidbin = (char *) hcmalloc (HCBUFSIZ_LARGE); + char *pidbin; - snprintf (pidbin, HCBUFSIZ_LARGE - 1, "/proc/%u/cmdline", rd->pid); + asprintf (&pidbin, "/proc/%u/cmdline", rd->pid); FILE *fd = fopen (pidbin, "rb"); @@ -355,18 +355,13 @@ int restore_ctx_init (hashcat_ctx_t *hashcat_ctx, int argc, char **argv) if (user_options->restore_file_path == NULL) { - restore_ctx->eff_restore_file = (char *) hcmalloc (HCBUFSIZ_TINY); - restore_ctx->new_restore_file = (char *) hcmalloc (HCBUFSIZ_TINY); - - snprintf (restore_ctx->eff_restore_file, HCBUFSIZ_TINY - 1, "%s/%s.restore", folder_config->session_dir, user_options->session); - snprintf (restore_ctx->new_restore_file, HCBUFSIZ_TINY - 1, "%s/%s.restore.new", folder_config->session_dir, user_options->session); + 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); } else { restore_ctx->eff_restore_file = hcstrdup (user_options->restore_file_path); - restore_ctx->new_restore_file = (char *) hcmalloc (HCBUFSIZ_TINY); - - snprintf (restore_ctx->new_restore_file, HCBUFSIZ_TINY - 1, "%s.new", user_options->restore_file_path); + asprintf (&restore_ctx->new_restore_file, "%s.new", user_options->restore_file_path); } restore_ctx->argc = argc; diff --git a/src/tuningdb.c b/src/tuningdb.c index 285542b57..b809e2503 100644 --- a/src/tuningdb.c +++ b/src/tuningdb.c @@ -67,9 +67,9 @@ int tuning_db_init (hashcat_ctx_t *hashcat_ctx) tuning_db->enabled = true; - char *tuning_db_file = (char *) hcmalloc (HCBUFSIZ_TINY); + char *tuning_db_file; - snprintf (tuning_db_file, HCBUFSIZ_TINY - 1, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); + asprintf (&tuning_db_file, "%s/%s", folder_config->shared_dir, TUNING_DB_FILE); FILE *fp = fopen (tuning_db_file, "rb"); diff --git a/src/wordlist.c b/src/wordlist.c index 2c79202dd..2d5903d3c 100644 --- a/src/wordlist.c +++ b/src/wordlist.c @@ -22,8 +22,7 @@ u32 convert_from_hex (hashcat_ctx_t *hashcat_ctx, char *line_buf, const u32 line if (user_options->hex_wordlist == true) { - u32 i; - u32 j; + size_t i, j; for (i = 0, j = 0; j < line_len; i += 1, j += 2) { @@ -44,8 +43,7 @@ u32 convert_from_hex (hashcat_ctx_t *hashcat_ctx, char *line_buf, const u32 line if (line_buf[4] != '[') return (line_len); if (line_buf[line_len - 1] != ']') return (line_len); - u32 i; - u32 j; + size_t i, j; for (i = 0, j = 5; j < line_len - 1; i += 1, j += 2) {