[Commits] Rev 3664: MDEV-4021 : Enable Ctrl-C handler when reading password, on Windows. in file:///H:/bzr/5.5/

Vladislav Vaintroub wlad at montyprogram.com
Thu Feb 21 22:46:52 EET 2013


At file:///H:/bzr/5.5/

------------------------------------------------------------
revno: 3664
revision-id: wlad at montyprogram.com-20130221204624-8s0i10nqbhqjmqrj
parent: wlad at montyprogram.com-20130220135243-4bz6hs88ohpktafj
fixes bug: https://mariadb.atlassian.net/browse/MDEV-4021
committer: Vladislav Vaintroub <wlad at montyprogram.com>
branch nick: 5.5
timestamp: Thu 2013-02-21 21:46:24 +0100
message:
  MDEV-4021 : Enable Ctrl-C handler when reading password, on Windows.
  
  Prior to this patch, _getch() was used to read password input from console. getch() has a property that it reads Ctrl-C as character with ASCII code 0x03, and disregards Ctrl-C handler for  current process. 
  The fix is to use ReadConsole() API instead of getch() , after setting console mode to ENABLE_PROCESSED_INPUT - this mode allows current process to handle Ctrl-C events.
-------------- next part --------------
=== modified file 'libmysql/get_password.c'
--- a/libmysql/get_password.c	2012-07-31 17:29:07 +0000
+++ b/libmysql/get_password.c	2013-02-21 20:46:24 +0000
@@ -62,13 +62,34 @@
 /* were just going to fake it here and get input from the keyboard */
 void get_tty_password_buff(const char *opt_message, char *to, size_t length)
 {
+  HANDLE consoleinput;
+  DWORD oldstate;
   char *pos=to,*end=to+length-1;
   int i=0;
+
+  consoleinput= GetStdHandle(STD_INPUT_HANDLE);
+  if (!consoleinput) 
+  {
+     /* This is a GUI application or service  without console input, bail out. */
+     *to= 0;
+     return;
+  }
   _cputs(opt_message ? opt_message : "Enter password: ");
+
+  /* 
+     Switch to raw mode (no line input, no echo input).
+     Allow Ctrl-C handler with ENABLE_PROCESSED_INPUT.
+  */
+  GetConsoleMode(consoleinput, &oldstate);
+  SetConsoleMode(consoleinput, ENABLE_PROCESSED_INPUT);
   for (;;)
   {
-    int tmp;
-    tmp=_getch();
+    char tmp;
+    DWORD chars_read;
+    if (!ReadConsole(consoleinput, &tmp, 1, &chars_read, NULL))
+      break;
+    if (chars_read == 0)
+      break;
     if (tmp == '\b' || tmp == 127)
     {
       if (pos != to)
@@ -78,13 +99,15 @@ void get_tty_password_buff(const char *o
         continue;
       }
     }
-    if (tmp == -1 || tmp == '\n' || tmp == '\r' || tmp == 3)
+    if (tmp == '\n' || tmp == '\r')
       break;
     if (iscntrl(tmp) || pos == end)
       continue;
     _cputs("*");
-    *(pos++) = (char)tmp;
+    *(pos++) = tmp;
   }
+  /* Reset console mode after password input. */ 
+  SetConsoleMode(consoleinput, oldstate);
   *pos=0;
   _cputs("\n");
 }



More information about the commits mailing list