Browse code

Add error reporting to get_console_input_win32().

When the function setup fails due to invalid file handles, or because
WriteFile(err, ...) fails (due to file handle corruption elsewhere),
the function used to silently "return false"

Change this to print a M_WARN|M_ERRNO message.

Also, change the function style to early-return style (= large diff, but
most are indent changes only).

v2: fix spurious "}" that was left over from change to early-return.

Signed-off-by: Gert Doering <gert@greenie.muc.de>
Acked-by: Lev Stipakov <lstipakov@gmail.com>
Message-Id: <20210618181246.30769-1-gert@greenie.muc.de>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg22577.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Gert Doering authored on 2021/06/19 03:12:46
Showing 1 changed files
... ...
@@ -75,65 +75,68 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const
75 75
     in = GetStdHandle(STD_INPUT_HANDLE);
76 76
     err = get_orig_stderr();
77 77
 
78
-    if (in != INVALID_HANDLE_VALUE
79
-        && err != INVALID_HANDLE_VALUE
80
-        && !win32_service_interrupt(&win32_signal)
81
-        && WriteFile(err, prompt, strlen(prompt), &len, NULL))
78
+    if (in == INVALID_HANDLE_VALUE
79
+        || err == INVALID_HANDLE_VALUE
80
+        || win32_service_interrupt(&win32_signal)
81
+        || !WriteFile(err, prompt, strlen(prompt), &len, NULL))
82 82
     {
83
-        bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
84
-        DWORD flags_save = 0;
85
-        int status = 0;
86
-        WCHAR *winput;
83
+        msg(M_WARN|M_ERRNO, "get_console_input_win32(): unexpected error");
84
+        return false;
85
+    }
87 86
 
88
-        if (is_console)
89
-        {
90
-            if (GetConsoleMode(in, &flags_save))
91
-            {
92
-                DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
93
-                if (echo)
94
-                {
95
-                    flags |= ENABLE_ECHO_INPUT;
96
-                }
97
-                SetConsoleMode(in, flags);
98
-            }
99
-            else
100
-            {
101
-                is_console = 0;
102
-            }
103
-        }
87
+    bool is_console = (GetFileType(in) == FILE_TYPE_CHAR);
88
+    DWORD flags_save = 0;
89
+    int status = 0;
90
+    WCHAR *winput;
104 91
 
105
-        if (is_console)
92
+    if (is_console)
93
+    {
94
+        if (GetConsoleMode(in, &flags_save))
106 95
         {
107
-            winput = malloc(capacity * sizeof(WCHAR));
108
-            if (winput == NULL)
96
+            DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT;
97
+            if (echo)
109 98
             {
110
-                return false;
99
+                flags |= ENABLE_ECHO_INPUT;
111 100
             }
112
-
113
-            status = ReadConsoleW(in, winput, capacity, &len, NULL);
114
-            WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
115
-            free(winput);
101
+            SetConsoleMode(in, flags);
116 102
         }
117 103
         else
118 104
         {
119
-            status = ReadFile(in, input, capacity, &len, NULL);
105
+            is_console = 0;
120 106
         }
107
+    }
121 108
 
122
-        string_null_terminate(input, (int)len, capacity);
123
-        chomp(input);
124
-
125
-        if (!echo)
126
-        {
127
-            WriteFile(err, "\r\n", 2, &len, NULL);
128
-        }
129
-        if (is_console)
130
-        {
131
-            SetConsoleMode(in, flags_save);
132
-        }
133
-        if (status && !win32_service_interrupt(&win32_signal))
109
+    if (is_console)
110
+    {
111
+        winput = malloc(capacity * sizeof(WCHAR));
112
+        if (winput == NULL)
134 113
         {
135
-            return true;
114
+            return false;
136 115
         }
116
+
117
+        status = ReadConsoleW(in, winput, capacity, &len, NULL);
118
+        WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL);
119
+        free(winput);
120
+    }
121
+    else
122
+    {
123
+        status = ReadFile(in, input, capacity, &len, NULL);
124
+    }
125
+
126
+    string_null_terminate(input, (int)len, capacity);
127
+    chomp(input);
128
+
129
+    if (!echo)
130
+    {
131
+        WriteFile(err, "\r\n", 2, &len, NULL);
132
+    }
133
+    if (is_console)
134
+    {
135
+        SetConsoleMode(in, flags_save);
136
+    }
137
+    if (status && !win32_service_interrupt(&win32_signal))
138
+    {
139
+        return true;
137 140
     }
138 141
 
139 142
     return false;