Skip to content

Commit

Permalink
Fix undefined behavior reinterpret_casts
Browse files Browse the repository at this point in the history
  • Loading branch information
Pokechu22 committed Jul 28, 2019
1 parent e8c2315 commit 586eb76
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext,
if (mode == WiimoteEmu::CameraLogic::IR_MODE_BASIC)
{
memset(ir_data, 0xFF, sizeof(WiimoteEmu::IRBasic) * 2);
auto* const ir_basic = reinterpret_cast<WiimoteEmu::IRBasic*>(ir_data);
std::array<WiimoteEmu::IRBasic, 2> ir_basic;
for (int i = 0; i < 2; ++i)
{
if (x[i * 2] < 1024 && y < 768)
Expand All @@ -480,13 +480,14 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext,
ir_basic[i].y2hi = y >> 8;
}
}
Common::BitCastPtr<std::array<WiimoteEmu::IRBasic, 2>>(ir_data) = ir_basic;
}
else
{
// TODO: this code doesnt work, resulting in no IR TAS inputs in e.g. wii sports menu when no
// remote extension is used
memset(ir_data, 0xFF, sizeof(WiimoteEmu::IRExtended) * 4);
auto* const ir_extended = reinterpret_cast<WiimoteEmu::IRExtended*>(ir_data);
std::array<WiimoteEmu::IRExtended, 4> ir_extended;
for (size_t i = 0; i < x.size(); ++i)
{
if (x[i] < 1024 && y < 768)
Expand All @@ -500,14 +501,17 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext,
ir_extended[i].size = 10;
}
}
Common::BitCastPtr<std::array<WiimoteEmu::IRExtended, 4>>(ir_data) = ir_extended;
}
}

if (rpt.HasExt() && m_nunchuk_stick_box->isVisible())
{
using WiimoteEmu::Nunchuk;

u8* const ext_data = rpt.GetExtDataPtr();

auto& nunchuk = *reinterpret_cast<WiimoteEmu::Nunchuk::DataFormat*>(ext_data);
Nunchuk::DataFormat nunchuk = Common::BitCastPtr<Nunchuk::DataFormat>(ext_data);

GetSpinBoxU8(m_nunchuk_stick_x_value, nunchuk.jx);
GetSpinBoxU8(m_nunchuk_stick_y_value, nunchuk.jy);
Expand All @@ -533,15 +537,18 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext,
GetButton<u8>(m_z_button, nunchuk.bt.hex, WiimoteEmu::Nunchuk::BUTTON_Z);
nunchuk.bt.hex ^= 0b11;

key.Encrypt(reinterpret_cast<u8*>(&nunchuk), 0, sizeof(nunchuk));
Common::BitCastPtr<Nunchuk::DataFormat>(ext_data) = nunchuk;
key.Encrypt(ext_data, 0, sizeof(Nunchuk::DataFormat));
}

if (m_classic_left_stick_box->isVisible())
{
using WiimoteEmu::Classic;

u8* const ext_data = rpt.GetExtDataPtr();
key.Decrypt(ext_data, 0, sizeof(Classic::DataFormat));

auto& cc = *reinterpret_cast<WiimoteEmu::Classic::DataFormat*>(ext_data);
key.Decrypt(reinterpret_cast<u8*>(&cc), 0, sizeof(cc));
Classic::DataFormat cc = Common::BitCastPtr<Classic::DataFormat>(ext_data);

cc.bt.hex ^= 0xFFFF;
GetButton<u16>(m_classic_a_button, cc.bt.hex, WiimoteEmu::Classic::BUTTON_A);
Expand Down Expand Up @@ -588,7 +595,8 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext,
cc.lt1 = lt & 0b111;
cc.lt2 = (lt >> 3) & 0b11;

key.Encrypt(reinterpret_cast<u8*>(&cc), 0, sizeof(cc));
Common::BitCastPtr<Classic::DataFormat>(ext_data) = cc;
key.Encrypt(ext_data, 0, sizeof(Classic::DataFormat));
}

if (rpt.HasExt() && m_balance_board_box->isVisible())
Expand Down

0 comments on commit 586eb76

Please sign in to comment.