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

Pass SettingsHandler buffers by const ref instead of rvalue ref (since the contents are copied either way) #12703

Merged
merged 1 commit into from Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions Source/Core/Common/SettingsHandler.cpp
Expand Up @@ -22,20 +22,20 @@ SettingsHandler::SettingsHandler()
Reset();
}

SettingsHandler::SettingsHandler(Buffer&& buffer)
SettingsHandler::SettingsHandler(const Buffer& buffer)
{
SetBytes(std::move(buffer));
SetBytes(buffer);
}

const SettingsHandler::Buffer& SettingsHandler::GetBytes() const
{
return m_buffer;
}

void SettingsHandler::SetBytes(Buffer&& buffer)
void SettingsHandler::SetBytes(const Buffer& buffer)
{
Reset();
m_buffer = std::move(buffer);
m_buffer = buffer;
Decrypt();
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Common/SettingsHandler.h
Expand Up @@ -25,12 +25,12 @@ class SettingsHandler

using Buffer = std::array<u8, SETTINGS_SIZE>;
SettingsHandler();
explicit SettingsHandler(Buffer&& buffer);
explicit SettingsHandler(const Buffer& buffer);

void AddSetting(std::string_view key, std::string_view value);

const Buffer& GetBytes() const;
void SetBytes(Buffer&& buffer);
void SetBytes(const Buffer& buffer);
std::string GetValue(std::string_view key) const;

void Decrypt();
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Boot/Boot_BS2Emu.cpp
Expand Up @@ -377,7 +377,7 @@ bool CBoot::SetupWiiMemory(Core::System& system, IOS::HLE::IOSC::ConsoleType con
IOS::HLE::FS::Mode::Read);
if (file && file->Read(data.data(), data.size()))
{
gen.SetBytes(std::move(data));
gen.SetBytes(data);
serno = gen.GetValue("SERNO");
model = gen.GetValue("MODEL");

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/IOS/DolphinDevice.cpp
Expand Up @@ -139,7 +139,7 @@ IPCReply GetRealProductCode(Core::System& system, const IOCtlVRequest& request)
return IPCReply(IPC_ENOENT);

Common::SettingsHandler gen;
gen.SetBytes(std::move(data));
gen.SetBytes(data);
const std::string code = gen.GetValue("CODE");

const size_t length = std::min<size_t>(request.io_vectors[0].size, code.length());
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/IOS/Network/KD/NetKDRequest.cpp
Expand Up @@ -931,7 +931,7 @@ IPCReply NetKDRequestDevice::HandleRequestRegisterUserId(const IOS::HLE::IOCtlRe
return IPCReply{IPC_SUCCESS};
}

const Common::SettingsHandler gen{std::move(data)};
const Common::SettingsHandler gen{data};
const std::string serno = gen.GetValue("SERNO");
const std::string form_data =
fmt::format("mlid=w{}&hdid={}&rgncd={}", m_config.Id(), m_ios.GetIOSC().GetDeviceId(), serno);
Expand Down Expand Up @@ -1079,7 +1079,7 @@ std::optional<IPCReply> NetKDRequestDevice::IOCtl(const IOCtlRequest& request)
Common::SettingsHandler::Buffer data;
if (file->Read(data.data(), data.size()))
{
const Common::SettingsHandler gen{std::move(data)};
const Common::SettingsHandler gen{data};
area = gen.GetValue("AREA");
model = gen.GetValue("MODEL");
}
Expand Down
23 changes: 8 additions & 15 deletions Source/UnitTests/Common/SettingsHandlerTest.cpp
Expand Up @@ -52,8 +52,7 @@ TEST(SettingsHandlerTest, EncryptSingleSetting)

TEST(SettingsHandlerTest, DecryptSingleSetting)
{
Common::SettingsHandler::Buffer buffer = BUFFER_A;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_A);
EXPECT_EQ(handler.GetValue("key"), "val");
}

Expand All @@ -70,22 +69,19 @@ TEST(SettingsHandlerTest, EncryptMultipleSettings)

TEST(SettingsHandlerTest, DecryptMultipleSettings)
{
Common::SettingsHandler::Buffer buffer = BUFFER_B;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_B);
EXPECT_EQ(handler.GetValue("key1"), "val1");
EXPECT_EQ(handler.GetValue("key2"), "val2");
EXPECT_EQ(handler.GetValue("foo"), "bar");
}

TEST(SettingsHandlerTest, SetBytesOverwritesExistingBuffer)
{
Common::SettingsHandler::Buffer buffer = BUFFER_A;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_A);
ASSERT_EQ(handler.GetValue("key"), "val");
ASSERT_EQ(handler.GetValue("foo"), "");

Common::SettingsHandler::Buffer buffer2 = BUFFER_B;
handler.SetBytes(std::move(buffer2));
handler.SetBytes(BUFFER_B);
EXPECT_EQ(handler.GetValue("foo"), "bar");
EXPECT_EQ(handler.GetValue("key"), "");
}
Expand All @@ -97,14 +93,13 @@ TEST(SettingsHandlerTest, GetValueOnSameInstance)
EXPECT_EQ(handler.GetValue("key"), "");

Common::SettingsHandler::Buffer buffer = handler.GetBytes();
handler.SetBytes(std::move(buffer));
handler.SetBytes(buffer);
EXPECT_EQ(handler.GetValue("key"), "val");
}

TEST(SettingsHandlerTest, GetValueAfterReset)
{
Common::SettingsHandler::Buffer buffer = BUFFER_A;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_A);
ASSERT_EQ(handler.GetValue("key"), "val");

handler.Reset();
Expand All @@ -131,14 +126,12 @@ TEST(SettingsHandlerTest, EncryptAddsLFOnNullCharTwice)

TEST(SettingsHandlerTest, DecryptSingleAddedLF)
{
Common::SettingsHandler::Buffer buffer = BUFFER_C;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_C);
EXPECT_EQ(handler.GetValue("\xFA"), "a");
}

TEST(SettingsHandlerTest, DecryptTwoAddedLFs)
{
Common::SettingsHandler::Buffer buffer = BUFFER_D;
Common::SettingsHandler handler(std::move(buffer));
Common::SettingsHandler handler(BUFFER_D);
EXPECT_EQ(handler.GetValue("\xFA\xE9"), "a");
}