1
0
Fork 0
forked from suyu/suyu

partition_filesystem: Ensure all class members of PartitionFilesystem are initialized

Previously is_hfs and pfs_header members wouldn't be initialized in the
constructor, as they were stored in locals instead. This would result in
things like GetName() and PrintDebugInfo() behaving incorrectly.

While we're at it, initialize the members to deterministic values as
well, in case loading ever fails.
This commit is contained in:
Lioncash 2018-07-18 23:25:07 -04:00
parent 1371e2fb6a
commit 9abc5763b6
2 changed files with 15 additions and 24 deletions

View file

@ -11,6 +11,11 @@
namespace FileSys { namespace FileSys {
bool PartitionFilesystem::Header::HasValidMagicValue() const {
return magic == Common::MakeMagic('H', 'F', 'S', '0') ||
magic == Common::MakeMagic('P', 'F', 'S', '0');
}
PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) { PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
// At least be as large as the header // At least be as large as the header
if (file->GetSize() < sizeof(Header)) { if (file->GetSize() < sizeof(Header)) {
@ -20,19 +25,17 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
// For cartridges, HFSs can get very large, so we need to calculate the size up to // For cartridges, HFSs can get very large, so we need to calculate the size up to
// the actual content itself instead of just blindly reading in the entire file. // the actual content itself instead of just blindly reading in the entire file.
Header pfs_header;
if (sizeof(Header) != file->ReadObject(&pfs_header)) { if (sizeof(Header) != file->ReadObject(&pfs_header)) {
status = Loader::ResultStatus::Error; status = Loader::ResultStatus::Error;
return; return;
} }
if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') && if (!pfs_header.HasValidMagicValue()) {
pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
status = Loader::ResultStatus::ErrorInvalidFormat; status = Loader::ResultStatus::ErrorInvalidFormat;
return; return;
} }
bool is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0'); is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry); size_t entry_size = is_hfs ? sizeof(HFSEntry) : sizeof(PFSEntry);
size_t metadata_size = size_t metadata_size =
@ -40,27 +43,13 @@ PartitionFilesystem::PartitionFilesystem(std::shared_ptr<VfsFile> file) {
// Actually read in now... // Actually read in now...
std::vector<u8> file_data = file->ReadBytes(metadata_size); std::vector<u8> file_data = file->ReadBytes(metadata_size);
const size_t total_size = file_data.size();
if (file_data.size() != metadata_size) { if (total_size != metadata_size) {
status = Loader::ResultStatus::Error; status = Loader::ResultStatus::Error;
return; return;
} }
size_t total_size = file_data.size();
if (total_size < sizeof(Header)) {
status = Loader::ResultStatus::Error;
return;
}
memcpy(&pfs_header, file_data.data(), sizeof(Header));
if (pfs_header.magic != Common::MakeMagic('H', 'F', 'S', '0') &&
pfs_header.magic != Common::MakeMagic('P', 'F', 'S', '0')) {
status = Loader::ResultStatus::ErrorInvalidFormat;
return;
}
is_hfs = pfs_header.magic == Common::MakeMagic('H', 'F', 'S', '0');
size_t entries_offset = sizeof(Header); size_t entries_offset = sizeof(Header);
size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size); size_t strtab_offset = entries_offset + (pfs_header.num_entries * entry_size);
content_offset = strtab_offset + pfs_header.strtab_size; content_offset = strtab_offset + pfs_header.strtab_size;

View file

@ -42,6 +42,8 @@ private:
u32_le num_entries; u32_le num_entries;
u32_le strtab_size; u32_le strtab_size;
INSERT_PADDING_BYTES(0x4); INSERT_PADDING_BYTES(0x4);
bool HasValidMagicValue() const;
}; };
static_assert(sizeof(Header) == 0x10, "PFS/HFS header structure size is wrong"); static_assert(sizeof(Header) == 0x10, "PFS/HFS header structure size is wrong");
@ -73,11 +75,11 @@ private:
#pragma pack(pop) #pragma pack(pop)
Loader::ResultStatus status; Loader::ResultStatus status{};
Header pfs_header; Header pfs_header{};
bool is_hfs; bool is_hfs = false;
size_t content_offset; size_t content_offset = 0;
std::vector<VirtualFile> pfs_files; std::vector<VirtualFile> pfs_files;
std::vector<VirtualDir> pfs_dirs; std::vector<VirtualDir> pfs_dirs;