Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 50485b3

Browse files
committed
Fix simple_prompt() to disable echo on Windows when stdin != terminal.
If echo = false, simple_prompt() is supposed to prevent echoing the input (for password input). However, the Windows implementation applied the mode change to STD_INPUT_HANDLE. That would not have the desired effect if stdin isn't actually the terminal, for instance if the user is piping something into psql. Fix it to apply the mode change to the correct input file, so that passwords do not echo in such cases. In passing, shorten and de-uglify this code by using #elif rather than an #if nest and removing some duplicated code. Back-patch to all supported versions. To simplify that, also back-patch the portions of commit 9daec77 that got rid of an unnecessary malloc/free in the same area. Matthew Stickney (cosmetic changes by me) Discussion: https://postgr.es/m/502a1fff-862b-da52-1031-f68df6ed5a2d@gmail.com
1 parent b929614 commit 50485b3

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

src/port/sprompt.c

+16-24
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,13 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
4040
FILE *termin,
4141
*termout;
4242

43-
#ifdef HAVE_TERMIOS_H
43+
#if defined(HAVE_TERMIOS_H)
4444
struct termios t_orig,
4545
t;
46-
#else
47-
#ifdef WIN32
46+
#elif defined(WIN32)
4847
HANDLE t = NULL;
4948
DWORD t_orig = 0;
5049
#endif
51-
#endif
5250

5351
#ifdef WIN32
5452

@@ -66,8 +64,11 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
6664
*
6765
* XXX fgets() still receives text in the console's input code page. This
6866
* makes non-ASCII credentials unportable.
67+
*
68+
* Unintuitively, we also open termin in mode "w+", even though we only
69+
* read it; that's needed for SetConsoleMode() to succeed.
6970
*/
70-
termin = fopen("CONIN$", "r");
71+
termin = fopen("CONIN$", "w+");
7172
termout = fopen("CONOUT$", "w+");
7273
#else
7374

@@ -99,29 +100,25 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
99100
termout = stderr;
100101
}
101102

102-
#ifdef HAVE_TERMIOS_H
103103
if (!echo)
104104
{
105+
#if defined(HAVE_TERMIOS_H)
106+
/* disable echo via tcgetattr/tcsetattr */
105107
tcgetattr(fileno(termin), &t);
106108
t_orig = t;
107109
t.c_lflag &= ~ECHO;
108110
tcsetattr(fileno(termin), TCSAFLUSH, &t);
109-
}
110-
#else
111-
#ifdef WIN32
112-
if (!echo)
113-
{
114-
/* get a new handle to turn echo off */
115-
t = GetStdHandle(STD_INPUT_HANDLE);
111+
#elif defined(WIN32)
112+
/* need the file's HANDLE to turn echo off */
113+
t = (HANDLE) _get_osfhandle(_fileno(termin));
116114

117115
/* save the old configuration first */
118116
GetConsoleMode(t, &t_orig);
119117

120118
/* set to the new mode */
121119
SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
122-
}
123-
#endif
124120
#endif
121+
}
125122

126123
if (prompt)
127124
{
@@ -151,24 +148,19 @@ simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
151148
/* remove trailing newline */
152149
destination[length - 1] = '\0';
153150

154-
#ifdef HAVE_TERMIOS_H
155151
if (!echo)
156152
{
153+
/* restore previous echo behavior, then echo \n */
154+
#if defined(HAVE_TERMIOS_H)
157155
tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
158156
fputs("\n", termout);
159157
fflush(termout);
160-
}
161-
#else
162-
#ifdef WIN32
163-
if (!echo)
164-
{
165-
/* reset to the original console mode */
158+
#elif defined(WIN32)
166159
SetConsoleMode(t, t_orig);
167160
fputs("\n", termout);
168161
fflush(termout);
169-
}
170-
#endif
171162
#endif
163+
}
172164

173165
if (termin != stdin)
174166
{

0 commit comments

Comments
 (0)