Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1K3psb-00027Z-Mw for qemu-devel@nongnu.org; Wed, 04 Jun 2008 06:06:05 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1K3psY-00026R-IL for qemu-devel@nongnu.org; Wed, 04 Jun 2008 06:06:03 -0400 Received: from [199.232.76.173] (port=43737 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1K3psX-00025a-Rl for qemu-devel@nongnu.org; Wed, 04 Jun 2008 06:06:01 -0400 Received: from savannah.gnu.org ([199.232.41.3]:48327 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1K3psX-0007gd-IH for qemu-devel@nongnu.org; Wed, 04 Jun 2008 06:06:01 -0400 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1K3psV-0007Wt-Tj for qemu-devel@nongnu.org; Wed, 04 Jun 2008 10:05:59 +0000 Received: from balrog by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1K3psV-0007Wo-L8 for qemu-devel@nongnu.org; Wed, 04 Jun 2008 10:05:59 +0000 MIME-Version: 1.0 Errors-To: balrog Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Andrzej Zaborowski Message-Id: Date: Wed, 04 Jun 2008 10:05:59 +0000 Subject: [Qemu-devel] [4657] Refactor and fix do_sendkey (Jan Kiszka). Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 4657 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=4657 Author: balrog Date: 2008-06-04 10:05:59 +0000 (Wed, 04 Jun 2008) Log Message: ----------- Refactor and fix do_sendkey (Jan Kiszka). Looking at the sendkey implementation, planning to enhance it with a hold time argument, I found some potential out-of-bound access and not very readable code. Here is a fix for the former and a (subjective) improvement of the latter. Signed-off-by: Jan Kiszka Modified Paths: -------------- trunk/monitor.c Modified: trunk/monitor.c =================================================================== --- trunk/monitor.c 2008-06-04 04:21:01 UTC (rev 4656) +++ trunk/monitor.c 2008-06-04 10:05:59 UTC (rev 4657) @@ -919,33 +919,38 @@ return -1; } -static void do_send_key(const char *string) +static void do_sendkey(const char *string) { - char keybuf[16], *q; uint8_t keycodes[16]; - const char *p; - int nb_keycodes, keycode, i; + int nb_keycodes = 0; + char keyname_buf[16]; + char *separator; + int keyname_len, keycode, i; - nb_keycodes = 0; - p = string; - while (*p != '\0') { - q = keybuf; - while (*p != '\0' && *p != '-') { - if ((q - keybuf) < sizeof(keybuf) - 1) { - *q++ = *p; + while (1) { + separator = strchr(string, '-'); + keyname_len = separator ? separator - string : strlen(string); + if (keyname_len > 0) { + pstrcpy(keyname_buf, sizeof(keyname_buf), string); + if (keyname_len > sizeof(keyname_buf) - 1) { + term_printf("invalid key: '%s...'\n", keyname_buf); + return; } - p++; + if (nb_keycodes == sizeof(keycodes)) { + term_printf("too many keys\n"); + return; + } + keyname_buf[keyname_len] = 0; + keycode = get_keycode(keyname_buf); + if (keycode < 0) { + term_printf("unknown key: '%s'\n", keyname_buf); + return; + } + keycodes[nb_keycodes++] = keycode; } - *q = '\0'; - keycode = get_keycode(keybuf); - if (keycode < 0) { - term_printf("unknown key: '%s'\n", keybuf); - return; - } - keycodes[nb_keycodes++] = keycode; - if (*p == '\0') + if (!separator) break; - p++; + string = separator + 1; } /* key down events */ for(i = 0; i < nb_keycodes; i++) { @@ -1347,7 +1352,7 @@ { "i", "/ii.", do_ioport_read, "/fmt addr", "I/O port read" }, - { "sendkey", "s", do_send_key, + { "sendkey", "s", do_sendkey, "keys", "send keys to the VM (e.g. 'sendkey ctrl-alt-f1')" }, { "system_reset", "", do_system_reset, "", "reset the system" },