fix: using a proper std::string to store KeyInfo buffers

This commit is contained in:
Luiz Sardinha 2026-02-12 18:40:44 +01:00 committed by Chris Rizzitello
parent 3c6aa9999b
commit 99da66dbf7
5 changed files with 9 additions and 31 deletions

View file

@ -30,8 +30,6 @@ IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(KeyID id, KeyModifierMask mask, Ke
info->m_mask = mask; info->m_mask = mask;
info->m_button = button; info->m_button = button;
info->m_count = count; info->m_count = count;
info->m_screens = nullptr;
info->m_screensBuffer[0] = '\0';
return info; return info;
} }
@ -39,43 +37,24 @@ IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(
KeyID id, KeyModifierMask mask, KeyButton button, int32_t count, const std::set<std::string> &destinations KeyID id, KeyModifierMask mask, KeyButton button, int32_t count, const std::set<std::string> &destinations
) )
{ {
std::string screens = join(destinations);
const char *buffer = screens.c_str();
// build structure
#if SYSAPI_WIN32
// On windows we use malloc to avoid random test failures
auto *info = (KeyInfo *)malloc(sizeof(KeyInfo) + screens.size());
#else
auto *info = new KeyInfo(); auto *info = new KeyInfo();
#endif
info->m_key = id; info->m_key = id;
info->m_mask = mask; info->m_mask = mask;
info->m_button = button; info->m_button = button;
info->m_count = count; info->m_count = count;
info->m_screens = info->m_screensBuffer; info->m_screens = join(destinations);
std::copy(buffer, buffer + screens.size() + 1, info->m_screensBuffer);
return info; return info;
} }
IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(const KeyInfo &x) IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(const KeyInfo &x)
{ {
auto bufferLen = strnlen(x.m_screensBuffer, SIZE_MAX);
#if SYSAPI_WIN32
// On windows we use malloc to avoid random test failures
auto info = (KeyInfo *)malloc(sizeof(KeyInfo) + bufferLen);
#else
auto *info = new KeyInfo(); auto *info = new KeyInfo();
#endif
info->m_key = x.m_key; info->m_key = x.m_key;
info->m_mask = x.m_mask; info->m_mask = x.m_mask;
info->m_button = x.m_button; info->m_button = x.m_button;
info->m_count = x.m_count; info->m_count = x.m_count;
info->m_screens = x.m_screens ? info->m_screensBuffer : nullptr; info->m_screens = x.m_screens;
memcpy(info->m_screensBuffer, x.m_screensBuffer, bufferLen + 1);
return info; return info;
} }
@ -107,7 +86,7 @@ bool IKeyState::KeyInfo::equal(const KeyInfo *a, const KeyInfo *b)
{ {
return ( return (
a->m_key == b->m_key && a->m_mask == b->m_mask && a->m_button == b->m_button && a->m_count == b->m_count && a->m_key == b->m_key && a->m_mask == b->m_mask && a->m_button == b->m_button && a->m_count == b->m_count &&
strcmp(a->m_screensBuffer, b->m_screensBuffer) == 0 a->m_screens == b->m_screens
); );
} }

View file

@ -45,8 +45,7 @@ public:
KeyModifierMask m_mask; KeyModifierMask m_mask;
KeyButton m_button; KeyButton m_button;
int32_t m_count; int32_t m_count;
char *m_screens; std::string m_screens;
char m_screensBuffer[1];
}; };
using KeyButtonSet = std::set<KeyButton>; using KeyButtonSet = std::set<KeyButton>;

View file

@ -469,8 +469,8 @@ std::string InputFilter::KeystrokeAction::format() const
); );
} else { } else {
return deskflow::string::sprintf( return deskflow::string::sprintf(
"%s(%s,%.*s)", type, deskflow::KeyMap::formatKey(m_keyInfo->m_key, m_keyInfo->m_mask).c_str(), "%s(%s,%s)", type, deskflow::KeyMap::formatKey(m_keyInfo->m_key, m_keyInfo->m_mask).c_str(),
strnlen(m_keyInfo->m_screens + 1, SIZE_MAX) - 1, m_keyInfo->m_screens + 1 m_keyInfo->m_screens.c_str()
); );
} }
} }

View file

@ -1219,13 +1219,13 @@ void Server::handleKeyDownEvent(const Event &event)
{ {
const auto *info = static_cast<IPlatformScreen::KeyInfo *>(event.getData()); const auto *info = static_cast<IPlatformScreen::KeyInfo *>(event.getData());
auto lang = AppUtil::instance().getCurrentLanguageCode(); auto lang = AppUtil::instance().getCurrentLanguageCode();
onKeyDown(info->m_key, info->m_mask, info->m_button, lang, info->m_screens); onKeyDown(info->m_key, info->m_mask, info->m_button, lang, info->m_screens.c_str());
} }
void Server::handleKeyUpEvent(const Event &event) void Server::handleKeyUpEvent(const Event &event)
{ {
auto *info = static_cast<IPlatformScreen::KeyInfo *>(event.getData()); auto *info = static_cast<IPlatformScreen::KeyInfo *>(event.getData());
onKeyUp(info->m_key, info->m_mask, info->m_button, info->m_screens); onKeyUp(info->m_key, info->m_mask, info->m_button, info->m_screens.c_str());
} }
void Server::handleKeyRepeatEvent(const Event &event) void Server::handleKeyRepeatEvent(const Event &event)

View file

@ -12,7 +12,7 @@
void IKeyStateTests::allocDestination() void IKeyStateTests::allocDestination()
{ {
auto info = IKeyState::KeyInfo::alloc(1, 2, 3, 4, {"test1", "test2"}); auto info = IKeyState::KeyInfo::alloc(1, 2, 3, 4, {"test1", "test2"});
QCOMPARE(info->m_screensBuffer, ":test1:test2:"); QCOMPARE(info->m_screens, ":test1:test2:");
} }
QTEST_MAIN(IKeyStateTests) QTEST_MAIN(IKeyStateTests)