From patchwork Fri Jun 18 08:12:46 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 1866 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director15.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id GKQ9Bm3izGD7CgAAIUCqbw (envelope-from ) for ; Fri, 18 Jun 2021 14:14:05 -0400 Received: from proxy3.mail.iad3b.rsapps.net ([172.31.255.6]) by director15.mail.ord1d.rsapps.net with LMTP id iITeBW3izGBHXgAAIcMcQg (envelope-from ) for ; Fri, 18 Jun 2021 14:14:05 -0400 Received: from smtp11.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy3.mail.iad3b.rsapps.net with LMTPS id iOV1OmzizGA4TQAAM8Wetg (envelope-from ) for ; Fri, 18 Jun 2021 14:14:04 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp11.gate.iad3b.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=greenie.muc.de X-Suspicious-Flag: YES X-Classification-ID: f6613892-d060-11eb-9c40-52540070b5bb-1-1 Received: from [216.105.38.7] ([216.105.38.7:52918] helo=lists.sourceforge.net) by smtp11.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 2B/2D-27773-B62ECC06; Fri, 18 Jun 2021 14:14:04 -0400 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1luIzE-00014p-M5; Fri, 18 Jun 2021 18:13:00 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1luIzC-00014g-EV for openvpn-devel@lists.sourceforge.net; Fri, 18 Jun 2021 18:12:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-Id:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=3tndcBUe1ULDCNfLlBGPh+Wt83gptI9yJnERA30FTVA=; b=mwYSTL+gnULWe/KCJFEk06H/ay 7MhYfmPE5/a9AaMHTxVoq1wCOfdle61ZQh4Vrms+1jZhPOjFbnEuSjgLZkAzxjukGSuPrk3s/iTm6 xqyT5plNdlpVNSUHMsYqntdOVQ9nhtocCiuUnpnPP1+r/hJwsUNR18eN+YXZ0uWHJcwE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=3tndcBUe1ULDCNfLlBGPh+Wt83gptI9yJnERA30FTVA=; b=Rt8ySpe6biDgWJvwvQvH1grWrF N3YSPylqaiLsWlXSuVc3a7V/F81VrOvYxGZ9ANGX7QRgqtLy1RS+w6aNCIHZXu8ct+FrezacfDX4B wMX2hcBnHmQNjpXPeejULFU8+AIvkXRmZMg2yi/oOD/YyCIAb12y2VRLK+LWnRHUIRbg=; Received: from dhcp-174.greenie.muc.de ([193.149.48.174] helo=blue.greenie.muc.de) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1luIz7-0000It-6C for openvpn-devel@lists.sourceforge.net; Fri, 18 Jun 2021 18:13:00 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.15.2/8.14.9) with ESMTP id 15IIClPb030817 for ; Fri, 18 Jun 2021 20:12:47 +0200 Received: (from gert@localhost) by blue.greenie.muc.de (8.15.2/8.14.9/Submit) id 15IIClRM030816 for openvpn-devel@lists.sourceforge.net; Fri, 18 Jun 2021 20:12:47 +0200 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Fri, 18 Jun 2021 20:12:46 +0200 Message-Id: <20210618181246.30769-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.26.3 In-Reply-To: <20210618171933.28985-1-gert@greenie.muc.de> References: <20210618171933.28985-1-gert@greenie.muc.de> MIME-Version: 1.0 X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 URIBL_BLOCKED ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [URIs: muc.de] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1luIz7-0000It-6C Subject: [Openvpn-devel] [PATCH v2] Add error reporting to get_console_input_win32(). X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox 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 Acked-by: Lev Stipakov --- src/openvpn/console_builtin.c | 95 ++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 46 deletions(-) diff --git a/src/openvpn/console_builtin.c b/src/openvpn/console_builtin.c index 3214cb5f..9bf36347 100644 --- a/src/openvpn/console_builtin.c +++ b/src/openvpn/console_builtin.c @@ -75,65 +75,68 @@ get_console_input_win32(const char *prompt, const bool echo, char *input, const in = GetStdHandle(STD_INPUT_HANDLE); err = get_orig_stderr(); - if (in != INVALID_HANDLE_VALUE - && err != INVALID_HANDLE_VALUE - && !win32_service_interrupt(&win32_signal) - && WriteFile(err, prompt, strlen(prompt), &len, NULL)) + if (in == INVALID_HANDLE_VALUE + || err == INVALID_HANDLE_VALUE + || win32_service_interrupt(&win32_signal) + || !WriteFile(err, prompt, strlen(prompt), &len, NULL)) { - bool is_console = (GetFileType(in) == FILE_TYPE_CHAR); - DWORD flags_save = 0; - int status = 0; - WCHAR *winput; + msg(M_WARN|M_ERRNO, "get_console_input_win32(): unexpected error"); + return false; + } - if (is_console) - { - if (GetConsoleMode(in, &flags_save)) - { - DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; - if (echo) - { - flags |= ENABLE_ECHO_INPUT; - } - SetConsoleMode(in, flags); - } - else - { - is_console = 0; - } - } + bool is_console = (GetFileType(in) == FILE_TYPE_CHAR); + DWORD flags_save = 0; + int status = 0; + WCHAR *winput; - if (is_console) + if (is_console) + { + if (GetConsoleMode(in, &flags_save)) { - winput = malloc(capacity * sizeof(WCHAR)); - if (winput == NULL) + DWORD flags = ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT; + if (echo) { - return false; + flags |= ENABLE_ECHO_INPUT; } - - status = ReadConsoleW(in, winput, capacity, &len, NULL); - WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL); - free(winput); + SetConsoleMode(in, flags); } else { - status = ReadFile(in, input, capacity, &len, NULL); + is_console = 0; } + } - string_null_terminate(input, (int)len, capacity); - chomp(input); - - if (!echo) - { - WriteFile(err, "\r\n", 2, &len, NULL); - } - if (is_console) - { - SetConsoleMode(in, flags_save); - } - if (status && !win32_service_interrupt(&win32_signal)) + if (is_console) + { + winput = malloc(capacity * sizeof(WCHAR)); + if (winput == NULL) { - return true; + return false; } + + status = ReadConsoleW(in, winput, capacity, &len, NULL); + WideCharToMultiByte(CP_UTF8, 0, winput, len, input, capacity, NULL, NULL); + free(winput); + } + else + { + status = ReadFile(in, input, capacity, &len, NULL); + } + + string_null_terminate(input, (int)len, capacity); + chomp(input); + + if (!echo) + { + WriteFile(err, "\r\n", 2, &len, NULL); + } + if (is_console) + { + SetConsoleMode(in, flags_save); + } + if (status && !win32_service_interrupt(&win32_signal)) + { + return true; } return false;