Make sure we don't try to use out-of-bounds icons/shapes for custom buttons

This commit is contained in:
Henrik Rydgård 2024-09-15 12:11:39 +02:00
parent 62656b6e6f
commit 047913ddd3
2 changed files with 22 additions and 0 deletions

View File

@ -882,6 +882,10 @@ UI::ViewGroup *CreatePadLayout(float xres, float yres, bool *pause, bool showPau
auto addCustomButton = [=](const ConfigCustomButton& cfg, const char *key, const ConfigTouchPos &touch) -> CustomButton * {
using namespace CustomKeyData;
if (touch.show) {
_dbg_assert_(cfg.shape < ARRAY_SIZE(customKeyShapes));
_dbg_assert_(cfg.image < ARRAY_SIZE(customKeyImages));
// Note: cfg.shape and cfg.image are bounds-checked elsewhere.
auto aux = root->Add(new CustomButton(cfg.key, key, cfg.toggle, cfg.repeat, controllMapper,
g_Config.iTouchButtonStyle == 0 ? customKeyShapes[cfg.shape].i : customKeyShapes[cfg.shape].l, customKeyShapes[cfg.shape].i,
customKeyImages[cfg.image].i, touch.scale, customKeyShapes[cfg.shape].d, buttonLayoutParams(touch)));
@ -940,7 +944,15 @@ UI::ViewGroup *CreatePadLayout(float xres, float yres, bool *pause, bool showPau
root->Add(new PSPStick(stickBg, "Right analog stick", stickImage, ImageID("I_STICK"), 1, g_Config.touchRightAnalogStick.scale, buttonLayoutParams(g_Config.touchRightAnalogStick)));
}
// Sanitize custom button images, while adding them.
for (int i = 0; i < Config::CUSTOM_BUTTON_COUNT; i++) {
if (g_Config.CustomButton[i].shape >= ARRAY_SIZE(CustomKeyData::customKeyShapes)) {
g_Config.CustomButton[i].shape = 0;
}
if (g_Config.CustomButton[i].image >= ARRAY_SIZE(CustomKeyData::customKeyImages)) {
g_Config.CustomButton[i].image = 0;
}
char temp[64];
snprintf(temp, sizeof(temp), "Custom %d button", i + 1);
addCustomButton(g_Config.CustomButton[i], temp, g_Config.touchCustom[i]);

View File

@ -514,6 +514,8 @@ void ControlLayoutView::CreateViews() {
auto addDragCustomKey = [&](ConfigTouchPos &pos, const char *key, const ConfigCustomButton& cfg) {
DragDropButton *b = nullptr;
if (pos.show) {
b = new DragDropButton(pos, key, g_Config.iTouchButtonStyle == 0 ? customKeyShapes[cfg.shape].i : customKeyShapes[cfg.shape].l, customKeyImages[cfg.image].i, bounds);
b->FlipImageH(customKeyShapes[cfg.shape].f);
b->SetAngle(customKeyImages[cfg.image].r, customKeyShapes[cfg.shape].r);
@ -523,6 +525,14 @@ void ControlLayoutView::CreateViews() {
};
for (int i = 0; i < Config::CUSTOM_BUTTON_COUNT; i++) {
// Similar to GamepadEmu, we sanitize the images for valid values.
if (g_Config.CustomButton[i].shape >= ARRAY_SIZE(CustomKeyData::customKeyShapes)) {
g_Config.CustomButton[i].shape = 0;
}
if (g_Config.CustomButton[i].image >= ARRAY_SIZE(CustomKeyData::customKeyImages)) {
g_Config.CustomButton[i].image = 0;
}
char temp[64];
snprintf(temp, sizeof(temp), "Custom %d button", i);
addDragCustomKey(g_Config.touchCustom[i], temp, g_Config.CustomButton[i]);