Replace some snprintf calls with asprintf.

Simplifies the code.
pull/925/head
Rosen Penev 8 years ago
parent cf4045cc6e
commit 36bd1c7dd9

@ -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");

@ -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);

@ -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;
}

@ -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
{

@ -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);

@ -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");

@ -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
{

@ -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
{

@ -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;

@ -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");

@ -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)
{

Loading…
Cancel
Save