From 586eb762dd26f57e95de82b01f08730a45885aea Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sat, 27 Jul 2019 21:59:33 -0700 Subject: [PATCH] Fix undefined behavior reinterpret_casts --- .../Core/DolphinQt/TAS/WiiTASInputWindow.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp index 8cbc47fe9bb8..a5de130c098e 100644 --- a/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp +++ b/Source/Core/DolphinQt/TAS/WiiTASInputWindow.cpp @@ -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(ir_data); + std::array ir_basic; for (int i = 0; i < 2; ++i) { if (x[i * 2] < 1024 && y < 768) @@ -480,13 +480,14 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext, ir_basic[i].y2hi = y >> 8; } } + Common::BitCastPtr>(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(ir_data); + std::array ir_extended; for (size_t i = 0; i < x.size(); ++i) { if (x[i] < 1024 && y < 768) @@ -500,14 +501,17 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext, ir_extended[i].size = 10; } } + Common::BitCastPtr>(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(ext_data); + Nunchuk::DataFormat nunchuk = Common::BitCastPtr(ext_data); GetSpinBoxU8(m_nunchuk_stick_x_value, nunchuk.jx); GetSpinBoxU8(m_nunchuk_stick_y_value, nunchuk.jy); @@ -533,15 +537,18 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext, GetButton(m_z_button, nunchuk.bt.hex, WiimoteEmu::Nunchuk::BUTTON_Z); nunchuk.bt.hex ^= 0b11; - key.Encrypt(reinterpret_cast(&nunchuk), 0, sizeof(nunchuk)); + Common::BitCastPtr(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(ext_data); - key.Decrypt(reinterpret_cast(&cc), 0, sizeof(cc)); + Classic::DataFormat cc = Common::BitCastPtr(ext_data); cc.bt.hex ^= 0xFFFF; GetButton(m_classic_a_button, cc.bt.hex, WiimoteEmu::Classic::BUTTON_A); @@ -588,7 +595,8 @@ void WiiTASInputWindow::GetValues(DataReportBuilder& rpt, int ext, cc.lt1 = lt & 0b111; cc.lt2 = (lt >> 3) & 0b11; - key.Encrypt(reinterpret_cast(&cc), 0, sizeof(cc)); + Common::BitCastPtr(ext_data) = cc; + key.Encrypt(ext_data, 0, sizeof(Classic::DataFormat)); } if (rpt.HasExt() && m_balance_board_box->isVisible())