gvdb-reader: refuse to open file with small header

Clean up the logic for dealing with invalid headers and include the case
where the file is too small to contain a fully-formed header.
This commit is contained in:
Ryan Lortie 2012-07-05 19:17:44 -04:00
parent 4e77b52ad8
commit ae3d42c60f

View File

@ -136,6 +136,7 @@ new_from_data (const void *data,
const char *filename, const char *filename,
GError **error) GError **error)
{ {
const struct gvdb_header *header;
GvdbTable *file; GvdbTable *file;
file = g_slice_new0 (GvdbTable); file = g_slice_new0 (GvdbTable);
@ -147,9 +148,10 @@ new_from_data (const void *data,
file->unref_user_data = unref; file->unref_user_data = unref;
file->user_data = user_data; file->user_data = user_data;
if (sizeof (struct gvdb_header) <= file->size) if (file->size < sizeof (struct gvdb_header))
{ goto invalid;
const struct gvdb_header *header = (gpointer) file->data;
header = (gpointer) file->data;
if (header->signature[0] == GVDB_SIGNATURE0 && if (header->signature[0] == GVDB_SIGNATURE0 &&
header->signature[1] == GVDB_SIGNATURE1 && header->signature[1] == GVDB_SIGNATURE1 &&
@ -162,26 +164,26 @@ new_from_data (const void *data,
file->byteswapped = TRUE; file->byteswapped = TRUE;
else else
{ goto invalid;
gvdb_table_setup_root (file, &header->root);
return file;
invalid:
if (filename) if (filename)
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "%s: invalid header", filename);
"%s: invalid header", filename);
else else
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, g_set_error_literal (error, G_FILE_ERROR, G_FILE_ERROR_INVAL, "invalid gvdb header");
"invalid gvdb header");
g_slice_free (GvdbTable, file); g_slice_free (GvdbTable, file);
if (unref) if (unref)
unref (user_data); unref (user_data);
return NULL; return NULL;
} }
gvdb_table_setup_root (file, &header->root);
}
return file;
}
/** /**
* gvdb_table_new: * gvdb_table_new:
* @filename: the path to the hash file * @filename: the path to the hash file