Skip to content

Commit

Permalink
Merge pull request #12703 from nlebeck/settingshandler-nomove-2
Browse files Browse the repository at this point in the history
Pass `SettingsHandler` buffers by const ref instead of rvalue ref (since the contents are copied either way)
  • Loading branch information
jordan-woyak committed Apr 13, 2024
2 parents 1bc6433 + d2b9673 commit 637ae12
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 25 deletions.
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");
}

0 comments on commit 637ae12

Please sign in to comment.