input-linux: fix Coverity warning
diff mbox

Message ID 1458129049-12484-1-git-send-email-kraxel@redhat.com
State New
Headers show

Commit Message

Gerd Hoffmann March 16, 2016, 11:50 a.m. UTC
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 ui/input-linux.c | 7 +++++++
 1 file changed, 7 insertions(+)

Comments

Stefan Weil March 16, 2016, 12:03 p.m. UTC | #1
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Am 16.03.2016 um 12:50 schrieb Gerd Hoffmann:
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> --- 
> ui/input-linux.c | 7 +++++++ 1 file changed, 7 insertions(+)
> 
> diff --git a/ui/input-linux.c b/ui/input-linux.c index
> 76a2abb..f884c00 100644 --- a/ui/input-linux.c +++
> b/ui/input-linux.c @@ -257,6 +257,13 @@ static void
> input_linux_event_keyboard(void *opaque) */ continue; } +
> if (event.code >= KEY_CNT) { +                /* +
> * Should not happen.  But better safe than sorry, +
> * and we make Coverity happy too. +                 */ +
> continue; +            } /* keep track of key state */ if
> (!il->keydown[event.code] && event.value) { il->keydown[event.code]
> = true;
> 

As it should not happen, I'd prefer this variant:

g_assert(event.code < KEY_CNT);

Regards,
Stefan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJW6UupAAoJEOCMIdVndFCtxc4QAJC9lsHMvJD2+o3k8F4MdqOx
IG2YY3EOPH1EClTWQ2JX1QM+YD3nxXXHFLAOl9xYo9tQwoD1/JGTjRMdIBZgTN7u
b5dLBBbAU+T/wEiEEVgBL2fACCoRFJtFTy9ZfbNe14jqsjbmArgXTpfnlQYJPbIa
KpVwj1fFgc11nwgn9VkxYmXK4/wlXfIiV86rzcXOfRZ+nGtsdwJbFkbNkEqc3mxk
q9155uo2u3Exqw+3NQNO+wqdmkEhuw1C2yrWGzCPUFHgiaHwatt2sTCW2S9uGvdH
PYlcsnErlFApQS3EE9nRJ4Jpqh1+jdbN/NvvAiL+Fh/EEReqzVbcD+Ga24uZB0eF
J/NgAEa6puPPjBnZ4CfnNGwd7sVfNhERULvso6EKTyn+eUqI6P3kl7VaiVAG7wpE
rYDwCWSEHzhUK5cuLbxpF2lF1+1Z9VkYRw/8EUjxZSp32VMjODQHw2jrwBQCMtWn
3MTXc0nAVfPowzVxjhk9mqlRzXauzbPL1Y0b6d74CADmEAzpxsrsD+oTV1ljG6rA
0E8sJVHB1k7I5lWM98K4Jc/uJ/gcynE8hDI2lnLqtmvCp0V/yP02v0cblAZb3Ldn
gDvEnQFoFgRGzm5ChXxAcMl9+CRScn9e94HzOTTJt3EEjQf9ifty4jEm7IxrNrd/
5eWjDuoctw4ZexgMir5H
=EIex
-----END PGP SIGNATURE-----
Gerd Hoffmann March 16, 2016, 12:35 p.m. UTC | #2
On Mi, 2016-03-16 at 13:03 +0100, Stefan Weil wrote:
> Am 16.03.2016 um 12:50 schrieb Gerd Hoffmann:
> > Signed-off-by: Gerd Hoffmann <kraxel@redhat.com> --- 
> > ui/input-linux.c | 7 +++++++ 1 file changed, 7 insertions(+)
> > 
> > diff --git a/ui/input-linux.c b/ui/input-linux.c index
> > 76a2abb..f884c00 100644 --- a/ui/input-linux.c +++
> > b/ui/input-linux.c @@ -257,6 +257,13 @@ static void
> > input_linux_event_keyboard(void *opaque) */ continue; } +
> > if (event.code >= KEY_CNT) { +                /* +
> > * Should not happen.  But better safe than sorry, +
> > * and we make Coverity happy too. +                 */ +
> > continue; +            } /* keep track of key state */ if
> > (!il->keydown[event.code] && event.value) { il->keydown[event.code]
> > = true;
> > 
> 
> As it should not happen, I'd prefer this variant:
> 
> g_assert(event.code < KEY_CNT);

Well, it is highly unlikely but not impossible.  KEY_CNT changed from
0x200 to 0x300 in kernel 2.6.28.  If (a) this happens again and (b) a
new key > 0x300 is pressed and (c) we didn't sync the linux headers yet
so qemu still has the old KEY_CNT value we could hit this.

cheers,
  Gerd

Patch
diff mbox

diff --git a/ui/input-linux.c b/ui/input-linux.c
index 76a2abb..f884c00 100644
--- a/ui/input-linux.c
+++ b/ui/input-linux.c
@@ -257,6 +257,13 @@  static void input_linux_event_keyboard(void *opaque)
                  */
                 continue;
             }
+            if (event.code >= KEY_CNT) {
+                /*
+                 * Should not happen.  But better safe than sorry,
+                 * and we make Coverity happy too.
+                 */
+                continue;
+            }
             /* keep track of key state */
             if (!il->keydown[event.code] && event.value) {
                 il->keydown[event.code] = true;