Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file read memory leak #750

Merged
merged 4 commits into from
Mar 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions src/core/hle/service/fs/file.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Citra Emulator Project
// Copyright Citra Emulator Project / Azahar Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

Expand Down Expand Up @@ -79,13 +79,13 @@ void File::Read(Kernel::HLERequestContext& ctx) {
if (!backend->AllowsCachedReads()) {
auto& buffer = rp.PopMappedBuffer();
IPC::RequestBuilder rb = rp.MakeBuilder(2, 2);
std::unique_ptr<u8*> data = std::make_unique<u8*>(static_cast<u8*>(operator new(length)));
const auto read = backend->Read(offset, length, *data);
std::unique_ptr<u8[]> data = std::make_unique_for_overwrite<u8[]>(length);
const auto read = backend->Read(offset, length, data.get());
if (read.Failed()) {
rb.Push(read.Code());
rb.Push<u32>(0);
} else {
buffer.Write(*data, 0, *read);
buffer.Write(data.get(), 0, *read);
rb.Push(ResultSuccess);
rb.Push<u32>(static_cast<u32>(*read));
}
Expand All @@ -106,7 +106,7 @@ void File::Read(Kernel::HLERequestContext& ctx) {
// Output
Result ret{0};
Kernel::MappedBuffer* buffer;
std::unique_ptr<u8*> data;
std::unique_ptr<u8[]> data;
std::size_t read_size;
};

Expand All @@ -122,10 +122,9 @@ void File::Read(Kernel::HLERequestContext& ctx) {
// LOG_DEBUG(Service_FS, "cache={}, offset={}, length={}", cache_ready, offset, length);
ctx.RunAsync(
[this, async_data](Kernel::HLERequestContext& ctx) {
async_data->data =
std::make_unique<u8*>(static_cast<u8*>(operator new(async_data->length)));
async_data->data = std::make_unique_for_overwrite<u8[]>(async_data->length);
const auto read =
backend->Read(async_data->offset, async_data->length, *async_data->data);
backend->Read(async_data->offset, async_data->length, async_data->data.get());
if (read.Failed()) {
async_data->ret = read.Code();
async_data->read_size = 0;
Expand Down Expand Up @@ -156,7 +155,7 @@ void File::Read(Kernel::HLERequestContext& ctx) {
rb.Push(async_data->ret);
rb.Push<u32>(0);
} else {
async_data->buffer->Write(*async_data->data, 0, async_data->read_size);
async_data->buffer->Write(async_data->data.get(), 0, async_data->read_size);
rb.Push(ResultSuccess);
rb.Push<u32>(static_cast<u32>(async_data->read_size));
}
Expand Down
Loading