1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-23 15:18:16 +00:00

Merge pull request #2943 from jtojanen/fix_filehandling

Avoid directly accessing file handle
This commit is contained in:
Jens Steube 2021-08-22 11:33:11 +02:00 committed by GitHub
commit 2d539b7e63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 35 deletions

View File

@ -22,11 +22,12 @@ int hc_fprintf (HCFILE *fp, const char *format, ...);
int hc_vfprintf (HCFILE *fp, const char *format, va_list ap);
int hc_fseek (HCFILE *fp, off_t offset, int whence);
void hc_rewind (HCFILE *fp);
int hc_fstat (HCFILE *fp, struct stat *buf);
off_t hc_ftell (HCFILE *fp);
int hc_fgetc (HCFILE *fp);
int hc_fileno (HCFILE *fp);
int hc_feof (HCFILE *fp);
void hc_fflush (HCFILE *fp);
void hc_fsync (HCFILE *fp);
void hc_fclose (HCFILE *fp);
int hc_fputc (int c, HCFILE *fp);
char *hc_fgets (char *buf, int len, HCFILE *fp);

View File

@ -96,19 +96,19 @@ bool hc_fopen (HCFILE *fp, const char *path, const char *mode)
close (fd_tmp);
}
if (fmode == -1)
{
fp->fd = open (path, oflag);
}
else
{
fp->fd = open (path, oflag, fmode);
}
if (fp->fd == -1) return false;
if (is_zip == false)
{
if (fmode == -1)
{
fp->fd = open (path, oflag);
}
else
{
fp->fd = open (path, oflag, fmode);
}
if (fp->fd == -1) return false;
if (is_gzip)
{
if ((fp->gfp = gzdopen (fp->fd, mode)) == NULL) return false;
@ -389,6 +389,13 @@ void hc_rewind (HCFILE *fp)
}
}
int hc_fstat (HCFILE *fp, struct stat *buf)
{
if (fp == NULL || buf == NULL || fp->fd == -1) return -1;
return fstat (fp->fd, buf);
}
off_t hc_ftell (HCFILE *fp)
{
off_t n = 0;
@ -544,13 +551,6 @@ int hc_fscanf (HCFILE *fp, const char *format, void *ptr)
return 1;
}
int hc_fileno (HCFILE *fp)
{
if (fp == NULL) return 1;
return fp->fd;
}
int hc_feof (HCFILE *fp)
{
int r = -1;
@ -590,6 +590,22 @@ void hc_fflush (HCFILE *fp)
}
}
void hc_fsync (HCFILE *fp)
{
if (fp == NULL) return;
if (fp->pfp)
{
#if defined (_WIN)
HANDLE h = (HANDLE) _get_osfhandle (fp->fd);
FlushFileBuffers (h);
#else
fsync (fp->fd);
#endif
}
}
void hc_fclose (HCFILE *fp)
{
if (fp == NULL) return;
@ -603,6 +619,8 @@ void hc_fclose (HCFILE *fp)
unzCloseCurrentFile (fp->ufp);
unzClose (fp->ufp);
close (fp->fd);
}
else if (fp->pfp)
{

View File

@ -21,7 +21,7 @@ int hc_lockfile (HCFILE *fp)
lock.l_type = F_WRLCK;
/* Needs this loop because a signal may interrupt a wait for lock */
while (fcntl (hc_fileno (fp), F_SETLKW, &lock))
while (fcntl (fp->fd, F_SETLKW, &lock))
{
if (errno != EINTR) return -1;
}
@ -39,7 +39,7 @@ int hc_unlockfile (HCFILE *fp)
lock.l_type = F_UNLCK;
if (fcntl (hc_fileno (fp), F_SETLK, &lock)) return -1;
if (fcntl (fp->fd, F_SETLK, &lock)) return -1;
return 0;
}

View File

@ -163,7 +163,7 @@ static int outfile_remove (hashcat_ctx_t *hashcat_ctx)
struct stat outfile_stat;
if (fstat (hc_fileno (&fp), &outfile_stat))
if (hc_fstat (&fp, &outfile_stat))
{
hc_fclose (&fp);

View File

@ -13,15 +13,6 @@
#include "folder.h"
#include "restore.h"
#if defined (_WIN)
static void fsync (int fd)
{
HANDLE h = (HANDLE) _get_osfhandle (fd);
FlushFileBuffers (h);
}
#endif
static int init_restore (hashcat_ctx_t *hashcat_ctx)
{
restore_ctx_t *restore_ctx = hashcat_ctx->restore_ctx;
@ -232,7 +223,7 @@ static int write_restore (hashcat_ctx_t *hashcat_ctx)
hc_fflush (&fp);
fsync (hc_fileno (&fp));
hc_fsync (&fp);
hc_fclose (&fp);

View File

@ -693,7 +693,7 @@ bool hc_same_files (char *file1, char *file2)
if (hc_fopen (&fp, file1, "r") == true)
{
if (fstat (hc_fileno (&fp), &tmpstat_file1))
if (hc_fstat (&fp, &tmpstat_file1))
{
hc_fclose (&fp);
@ -707,7 +707,7 @@ bool hc_same_files (char *file1, char *file2)
if (hc_fopen (&fp, file2, "r") == true)
{
if (fstat (hc_fileno (&fp), &tmpstat_file2))
if (hc_fstat (&fp, &tmpstat_file2))
{
hc_fclose (&fp);

View File

@ -343,7 +343,7 @@ int count_words (hashcat_ctx_t *hashcat_ctx, HCFILE *fp, const char *dictfile, u
memset (&d, 0, sizeof (d));
if (fstat (hc_fileno (fp), &d.stat))
if (hc_fstat (fp, &d.stat))
{
*result = 0;