libinput: Fix key mapping
authorLaszlo Agocs <laszlo.agocs@theqtcompany.com>
Tue, 18 Aug 2015 09:51:45 +0000 (11:51 +0200)
committerLaszlo Agocs <laszlo.agocs@theqtcompany.com>
Fri, 21 Aug 2015 21:02:56 +0000 (21:02 +0000)
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 <shawn.rutledge@theqtcompany.com>
src/platformsupport/input/libinput/qlibinputkeyboard.cpp

index 3bb9107e16df8877fba515b09761d26dbcede5be..ec7ea7ef0d59c99f98184e961a3a5a104f651195 100644 (file)
@@ -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<char, 32> 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);