From 99da66dbf763e9788d62e6e737507496af4278c8 Mon Sep 17 00:00:00 2001 From: Luiz Sardinha Date: Thu, 12 Feb 2026 18:40:44 +0100 Subject: [PATCH] fix: using a proper std::string to store KeyInfo buffers --- src/lib/deskflow/IKeyState.cpp | 27 +++-------------------- src/lib/deskflow/IKeyState.h | 3 +-- src/lib/server/InputFilter.cpp | 4 ++-- src/lib/server/Server.cpp | 4 ++-- src/unittests/deskflow/IKeyStateTests.cpp | 2 +- 5 files changed, 9 insertions(+), 31 deletions(-) diff --git a/src/lib/deskflow/IKeyState.cpp b/src/lib/deskflow/IKeyState.cpp index 553c19809..8b166c2fd 100644 --- a/src/lib/deskflow/IKeyState.cpp +++ b/src/lib/deskflow/IKeyState.cpp @@ -30,8 +30,6 @@ IKeyState::KeyInfo *IKeyState::KeyInfo::alloc(KeyID id, KeyModifierMask mask, Ke info->m_mask = mask; info->m_button = button; info->m_count = count; - info->m_screens = nullptr; - info->m_screensBuffer[0] = '\0'; return info; } @@ -39,43 +37,24 @@ IKeyState::KeyInfo *IKeyState::KeyInfo::alloc( KeyID id, KeyModifierMask mask, KeyButton button, int32_t count, const std::set &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(); -#endif - info->m_key = id; info->m_mask = mask; info->m_button = button; info->m_count = count; - info->m_screens = info->m_screensBuffer; - std::copy(buffer, buffer + screens.size() + 1, info->m_screensBuffer); + info->m_screens = join(destinations); return info; } 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(); -#endif info->m_key = x.m_key; info->m_mask = x.m_mask; info->m_button = x.m_button; info->m_count = x.m_count; - info->m_screens = x.m_screens ? info->m_screensBuffer : nullptr; - memcpy(info->m_screensBuffer, x.m_screensBuffer, bufferLen + 1); + info->m_screens = x.m_screens; return info; } @@ -107,7 +86,7 @@ bool IKeyState::KeyInfo::equal(const KeyInfo *a, const KeyInfo *b) { 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 && - strcmp(a->m_screensBuffer, b->m_screensBuffer) == 0 + a->m_screens == b->m_screens ); } diff --git a/src/lib/deskflow/IKeyState.h b/src/lib/deskflow/IKeyState.h index cad575a64..ed034fc0c 100644 --- a/src/lib/deskflow/IKeyState.h +++ b/src/lib/deskflow/IKeyState.h @@ -45,8 +45,7 @@ public: KeyModifierMask m_mask; KeyButton m_button; int32_t m_count; - char *m_screens; - char m_screensBuffer[1]; + std::string m_screens; }; using KeyButtonSet = std::set; diff --git a/src/lib/server/InputFilter.cpp b/src/lib/server/InputFilter.cpp index e3e7e6da9..b9227dec0 100644 --- a/src/lib/server/InputFilter.cpp +++ b/src/lib/server/InputFilter.cpp @@ -469,8 +469,8 @@ std::string InputFilter::KeystrokeAction::format() const ); } else { return deskflow::string::sprintf( - "%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 + "%s(%s,%s)", type, deskflow::KeyMap::formatKey(m_keyInfo->m_key, m_keyInfo->m_mask).c_str(), + m_keyInfo->m_screens.c_str() ); } } diff --git a/src/lib/server/Server.cpp b/src/lib/server/Server.cpp index fb8d676c0..3ddf06537 100644 --- a/src/lib/server/Server.cpp +++ b/src/lib/server/Server.cpp @@ -1219,13 +1219,13 @@ void Server::handleKeyDownEvent(const Event &event) { const auto *info = static_cast(event.getData()); 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) { auto *info = static_cast(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) diff --git a/src/unittests/deskflow/IKeyStateTests.cpp b/src/unittests/deskflow/IKeyStateTests.cpp index d9a3e160d..d1832535d 100644 --- a/src/unittests/deskflow/IKeyStateTests.cpp +++ b/src/unittests/deskflow/IKeyStateTests.cpp @@ -12,7 +12,7 @@ void IKeyStateTests::allocDestination() { 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)