From b409b1a0e423fe690f038ca0140830b13970fa4d Mon Sep 17 00:00:00 2001 From: Laszlo Agocs Date: Tue, 18 Aug 2015 11:51:45 +0200 Subject: [PATCH] libinput: Fix key mapping Prevent generating 2 character long 'text' strings with some garbage as second char. This matches how xcb works. Change-Id: I88a248a89c80b0e100c1c4871cfab4f2c287535e Reviewed-by: Shawn Rutledge --- .../input/libinput/qlibinputkeyboard.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/platformsupport/input/libinput/qlibinputkeyboard.cpp b/src/platformsupport/input/libinput/qlibinputkeyboard.cpp index 3bb9107e16..ec7ea7ef0d 100644 --- a/src/platformsupport/input/libinput/qlibinputkeyboard.cpp +++ b/src/platformsupport/input/libinput/qlibinputkeyboard.cpp @@ -180,10 +180,13 @@ void QLibInputKeyboard::processKey(libinput_event_keyboard *e) const uint32_t k = libinput_event_keyboard_get_key(e) + 8; const bool pressed = libinput_event_keyboard_get_key_state(e) == LIBINPUT_KEY_STATE_PRESSED; - QByteArray chars; - chars.resize(1 + xkb_state_key_get_utf8(m_state, k, Q_NULLPTR, 0)); - xkb_state_key_get_utf8(m_state, k, chars.data(), chars.size()); - const QString text = QString::fromUtf8(chars); + QVarLengthArray chars(32); + const int size = xkb_state_key_get_utf8(m_state, k, chars.data(), chars.size()); + if (Q_UNLIKELY(size + 1 > chars.size())) { // +1 for NUL + chars.resize(size + 1); + xkb_state_key_get_utf8(m_state, k, chars.data(), chars.size()); + } + const QString text = QString::fromUtf8(chars.constData(), size); const xkb_keysym_t sym = xkb_state_key_get_one_sym(m_state, k);