1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-11-23 12:22:08 +00:00

fs_mitm: Fix mismatched new[] / delete (#389)

* fs.mitm: Fix mismatched new[] / delete

Using delete instead of delete[] on a pointer given by new[] is
undefined behaviour.

For memory sources, malloc/free are used because cleaning up is tricky
when data can be either allocated with new (RomfsHeader) or new[]
(metadata).

* set.mitm: Fix mismatched new[] / delete
This commit is contained in:
Léo Lam 2019-03-08 16:25:33 +01:00 committed by SciresM
parent 66560d0a7b
commit b6d3df3335
3 changed files with 16 additions and 15 deletions

View file

@ -49,7 +49,7 @@ void RomFSBuildContext::VisitDirectory(FsFileSystem *filesys, RomFSBuildDirector
strcat(child->path + parent->path_len, this->dir_entry.name); strcat(child->path + parent->path_len, this->dir_entry.name);
if (!this->AddDirectory(parent, child, NULL)) { if (!this->AddDirectory(parent, child, NULL)) {
delete child->path; delete[] child->path;
delete child; delete child;
} else { } else {
child_dirs.push_back(child); child_dirs.push_back(child);
@ -72,7 +72,7 @@ void RomFSBuildContext::VisitDirectory(FsFileSystem *filesys, RomFSBuildDirector
child->size = this->dir_entry.fileSize; child->size = this->dir_entry.fileSize;
if (!this->AddFile(parent, child)) { if (!this->AddFile(parent, child)) {
delete child->path; delete[] child->path;
delete child; delete child;
} }
} else { } else {
@ -125,7 +125,7 @@ void RomFSBuildContext::VisitDirectory(RomFSBuildDirectoryContext *parent, u32 p
child->source = this->cur_source_type; child->source = this->cur_source_type;
child->orig_offset = cur_file->offset; child->orig_offset = cur_file->offset;
if (!this->AddFile(parent, child)) { if (!this->AddFile(parent, child)) {
delete child->path; delete[] child->path;
delete child; delete child;
} }
if (cur_file->sibling == ROMFS_ENTRY_EMPTY) { if (cur_file->sibling == ROMFS_ENTRY_EMPTY) {
@ -153,7 +153,7 @@ void RomFSBuildContext::VisitDirectory(RomFSBuildDirectoryContext *parent, u32 p
RomFSBuildDirectoryContext *real = NULL; RomFSBuildDirectoryContext *real = NULL;
if (!this->AddDirectory(parent, child, &real)) { if (!this->AddDirectory(parent, child, &real)) {
delete child->path; delete[] child->path;
delete child; delete child;
} }
if (real == NULL) { if (real == NULL) {
@ -246,8 +246,10 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
this->file_hash_table_size = 4 * file_hash_table_entry_count; this->file_hash_table_size = 4 * file_hash_table_entry_count;
/* Assign metadata pointers */ /* Assign metadata pointers */
RomFSHeader *header = new RomFSHeader({0}); auto *header = reinterpret_cast<RomFSHeader*>(std::malloc(sizeof(RomFSHeader)));
u8 *metadata = new u8[this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size]; *header = {};
const size_t metadata_size = this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size;
u8 *metadata = reinterpret_cast<u8*>(std::malloc(metadata_size));
u32 *dir_hash_table = (u32 *)((uintptr_t)metadata); u32 *dir_hash_table = (u32 *)((uintptr_t)metadata);
RomFSDirectoryEntry *dir_table = (RomFSDirectoryEntry *)((uintptr_t)dir_hash_table + this->dir_hash_table_size); RomFSDirectoryEntry *dir_table = (RomFSDirectoryEntry *)((uintptr_t)dir_hash_table + this->dir_hash_table_size);
u32 *file_hash_table = (u32 *)((uintptr_t)dir_table + this->dir_table_size); u32 *file_hash_table = (u32 *)((uintptr_t)dir_table + this->dir_table_size);
@ -372,7 +374,7 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
/* Delete directories. */ /* Delete directories. */
for (const auto &it : this->directories) { for (const auto &it : this->directories) {
cur_dir = it.second; cur_dir = it.second;
delete cur_dir->path; delete[] cur_dir->path;
delete cur_dir; delete cur_dir;
} }
this->root = NULL; this->root = NULL;
@ -381,7 +383,7 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
/* Delete files. */ /* Delete files. */
for (const auto &it : this->files) { for (const auto &it : this->files) {
cur_file = it.second; cur_file = it.second;
delete cur_file->path; delete[] cur_file->path;
delete cur_file; delete cur_file;
} }
this->files.clear(); this->files.clear();
@ -398,12 +400,10 @@ void RomFSBuildContext::Build(std::vector<RomFSSourceInfo> *out_infos) {
header->file_hash_table_ofs = header->dir_table_ofs + header->dir_table_size; header->file_hash_table_ofs = header->dir_table_ofs + header->dir_table_size;
header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size; header->file_table_ofs = header->file_hash_table_ofs + header->file_hash_table_size;
const size_t metadata_size = this->dir_hash_table_size + this->dir_table_size + this->file_hash_table_size + this->file_table_size;
/* Try to save metadata to the SD card, to save on memory space. */ /* Try to save metadata to the SD card, to save on memory space. */
if (R_SUCCEEDED(Utils::SaveSdFileForAtmosphere(this->title_id, ROMFS_METADATA_FILE_PATH, metadata, metadata_size))) { if (R_SUCCEEDED(Utils::SaveSdFileForAtmosphere(this->title_id, ROMFS_METADATA_FILE_PATH, metadata, metadata_size))) {
out_infos->emplace_back(header->dir_hash_table_ofs, metadata_size, RomFSDataSource::MetaData); out_infos->emplace_back(header->dir_hash_table_ofs, metadata_size, RomFSDataSource::MetaData);
delete metadata; std::free(metadata);
} else { } else {
out_infos->emplace_back(header->dir_hash_table_ofs, metadata_size, metadata, RomFSDataSource::Memory); out_infos->emplace_back(header->dir_hash_table_ofs, metadata_size, metadata, RomFSDataSource::Memory);
} }

View file

@ -15,6 +15,7 @@
*/ */
#pragma once #pragma once
#include <cstdlib>
#include <switch.h> #include <switch.h>
#include <map> #include <map>
@ -120,10 +121,10 @@ struct RomFSSourceInfo {
case RomFSDataSource::MetaData: case RomFSDataSource::MetaData:
break; break;
case RomFSDataSource::LooseFile: case RomFSDataSource::LooseFile:
delete this->loose_source_info.path; delete[] this->loose_source_info.path;
break; break;
case RomFSDataSource::Memory: case RomFSDataSource::Memory:
delete this->memory_source_info.data; std::free((void*)this->memory_source_info.data);
break; break;
default: default:
fatalSimple(0xF601); fatalSimple(0xF601);

View file

@ -257,7 +257,7 @@ void SettingsItemManager::LoadConfiguration() {
char *config_buf = new char[0x10000]; char *config_buf = new char[0x10000];
std::memset(config_buf, 0, 0x10000); std::memset(config_buf, 0, 0x10000);
ON_SCOPE_EXIT { ON_SCOPE_EXIT {
delete config_buf; delete[] config_buf;
}; };
/* Read from file. */ /* Read from file. */