Skip to content

Commit

Permalink
fix: Valgrind growls at hex dump and validation buf
Browse files Browse the repository at this point in the history
  • Loading branch information
abelcheung committed Dec 17, 2023
1 parent 00c4a7e commit 6a67d4f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
16 changes: 10 additions & 6 deletions src/rifiuti-vista.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _validate_index_file (const char *filename,
GError **error)
{
gsize expect_sz;
char *buf;
char *buf = NULL;
uint32_t pathlen;

g_return_val_if_fail (filename && *filename, FALSE);
Expand All @@ -48,7 +48,7 @@ _validate_index_file (const char *filename,
g_debug ("Start file validation for '%s'...", filename);

if (! g_file_get_contents (filename, &buf, bufsize, error))
return FALSE;
goto validate_fail;

g_debug ("Read '%s' successfully, size = %" G_GSIZE_FORMAT,
filename, *bufsize);
Expand All @@ -60,7 +60,7 @@ _validate_index_file (const char *filename,
*bufsize, (gsize) VERSION1_FILENAME_OFFSET);
g_set_error_literal (error, R2_REC_ERROR, R2_REC_ERROR_IDX_SIZE_INVALID,
_("File is prematurely truncated, or not a $Recycle.bin index."));
return FALSE;
goto validate_fail;
}

copy_field (ver, VERSION_OFFSET, FILESIZE_OFFSET);
Expand All @@ -79,7 +79,7 @@ _validate_index_file (const char *filename,
", expected = %" G_GSIZE_FORMAT " or %" G_GSIZE_FORMAT, *bufsize, expect_sz, expect_sz - 1);
g_set_error (error, R2_REC_ERROR, R2_REC_ERROR_IDX_SIZE_INVALID,
"%s", _("Might be an index file, but file size is unexpected."));
return FALSE;
goto validate_fail;
}
break;

Expand All @@ -99,7 +99,7 @@ _validate_index_file (const char *filename,
*bufsize, expect_sz);
g_set_error (error, R2_REC_ERROR, R2_REC_ERROR_IDX_SIZE_INVALID,
"%s", _("Might be an index file, but file size is unexpected."));
return FALSE;
goto validate_fail;
}
break;

Expand All @@ -112,12 +112,16 @@ _validate_index_file (const char *filename,
g_set_error (error, R2_REC_ERROR,
R2_REC_ERROR_VER_UNSUPPORTED,
"%s", _("File is not a $Recycle.bin index"));
return FALSE;
goto validate_fail;
}

*filebuf = buf;
g_debug ("Finished file validation for '%s'", filename);
return TRUE;

validate_fail:
g_free (buf);
return FALSE;
}


Expand Down
16 changes: 9 additions & 7 deletions src/rifiuti.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ _validate_index_file (const char *filename,
FILE **infile,
GError **error)
{
void *buf;
void *buf = NULL;
FILE *fp = NULL;
uint32_t ver;
int e;
Expand All @@ -65,7 +65,7 @@ _validate_index_file (const char *filename,
g_set_error_literal (error, R2_FATAL_ERROR,
R2_FATAL_ERROR_ILLEGAL_DATA,
_("File is prematurely truncated, or not an INFO2 index."));
goto validation_broken;
goto validation_fail;
}

copy_field (&ver, VERSION_OFFSET, KEPT_ENTRY_OFFSET);
Expand All @@ -82,6 +82,7 @@ _validate_index_file (const char *filename,
meta->recordsize = GUINT32_FROM_LE (meta->recordsize);

g_free (buf);
buf = NULL;

switch (meta->recordsize)
{
Expand All @@ -93,7 +94,7 @@ _validate_index_file (const char *filename,
{
g_set_error (error, R2_FATAL_ERROR, R2_FATAL_ERROR_ILLEGAL_DATA,
"Illegal INFO2 version %" PRIu32, ver);
goto validation_broken;
goto validation_fail;
}

if (!legacy_encoding)
Expand All @@ -104,7 +105,7 @@ _validate_index_file (const char *filename,
"without Unicode file name (Windows ME or earlier). "
"Please specify codepage of concerned system with "
"'-l' option.");
goto validation_broken;
goto validation_fail;
}
break;

Expand All @@ -114,24 +115,25 @@ _validate_index_file (const char *filename,
{
g_set_error (error, R2_FATAL_ERROR, R2_FATAL_ERROR_ILLEGAL_DATA,
"Illegal INFO2 version %" PRIu32, ver);
goto validation_broken;
goto validation_fail;
}
break;

default:
g_set_error (error, R2_FATAL_ERROR, R2_FATAL_ERROR_ILLEGAL_DATA,
_("Illegal INFO2 of record size %" G_GSIZE_FORMAT),
meta->recordsize);
goto validation_broken;
goto validation_fail;
}

rewind (fp);
*infile = fp;
meta->version = ver;
return TRUE;

validation_broken:
validation_fail:

g_free (buf);
fclose (fp);
return FALSE;
}
Expand Down
9 changes: 7 additions & 2 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1366,6 +1366,7 @@ _print_json_record (rbin_struct *record,
g_free (tmp);
g_free (path);
g_free (dt_str);
g_string_free (s, TRUE);
}


Expand Down Expand Up @@ -1594,7 +1595,7 @@ hexdump (void *start,
{
GString *s = g_string_new ("");
size_t i = 0;
do
while (TRUE)
{
if (i % 16 == 0)
{
Expand All @@ -1605,7 +1606,11 @@ hexdump (void *start,
}
g_string_append_printf (s, "%04zX ", i);
}
if (i >= size)
break;
g_string_append_printf (s, "%02" PRIX8 " ", *(uint8_t *) (start+i));
i++;
}
while (i++ < size);

g_string_free (s, TRUE);
}

0 comments on commit 6a67d4f

Please sign in to comment.