[Openvpn-devel,12/13] Memory size arithmetic reviewed according to 64-bit MSVC complaints
| Message ID | 20171010231130.6832-12-simon@rozman.si |
|---|---|
| State | Superseded |
| Headers |
Return-Path: <openvpn-devel-bounces@lists.sourceforge.net> Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director6.mail.ord1d.rsapps.net ([172.30.157.13]) by backend31.mail.ord1d.rsapps.net (Dovecot) with LMTP id OAaRH95T3Vn0TAAAgoeIoA for <patchwork@openvpn.net>; Tue, 10 Oct 2017 19:12:30 -0400 Received: from proxy5.mail.ord1d.rsapps.net ([172.30.157.44]) by director6.mail.ord1d.rsapps.net (Dovecot) with LMTP id 2bLyLN5T3VnhPQAAhgvE6Q ; Tue, 10 Oct 2017 19:12:30 -0400 Received: from smtp1.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy5.mail.ord1d.rsapps.net (Dovecot) with LMTP id 9azlInxT3VmHQQAA8Zzt7w ; Tue, 10 Oct 2017 19:12:30 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-MessageSniffer-Scan-Result: 0 X-MessageSniffer-Rules: 0-298-1000-1198-w 0-298-1000-1562-w 0-298-0-12983-f X-CMAE-Scan-Result: 0 X-CNFS-Analysis: v=2.2 cv=bqYOPwSi c=1 sm=1 tr=0 a=Q8DxjiC8O3VT/NpP1XjEZQ==:117 a=Q8DxjiC8O3VT/NpP1XjEZQ==:17 a=kj9zAlcOel0A:10 a=02M-m0pO-4AA:10 a=WiVod9pSvdkA:10 a=9sSjY8p1AAAA:8 a=P_JWiMecAAAA:8 a=FP58Ms26AAAA:8 a=G9hyuhLczAr52wfc7ekA:9 a=CjuIK1q_8ugA:10 a=-FEs8UIgK8oA:10 a=NWVoK91CQyQA:10 a=ub54wNWiXv_DzeFsgEJW:22 a=D0-HAvA3Hk9NMREbgwuX:22 X-Orig-To: justin@openvpn.net X-Originating-Ip: [216.34.181.88] Authentication-Results: smtp1.gate.ord1c.rsapps.net; iprev=pass policy.iprev="216.34.181.88"; 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; dkim=fail (signature verification failed) header.d=rozman.si; dmarc=fail (p=none; dis=none) header.from=rozman.si X-Classification-ID: 7c9eb5b4-ae10-11e7-9a71-842b2b47c027-1-1 Received: from [216.34.181.88] ([216.34.181.88:15698] helo=lists.sourceforge.net) by smtp1.gate.ord1c.rsapps.net (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 95/52-29879-DD35DD95; Tue, 10 Oct 2017 19:12:29 -0400 Received: from localhost ([127.0.0.1] helo=sfs-ml-4.v29.ch3.sourceforge.com) by sfs-ml-4.v29.ch3.sourceforge.com with esmtp (Exim 4.89) (envelope-from <openvpn-devel-bounces@lists.sourceforge.net>) id 1e23gt-00008a-7Q; Tue, 10 Oct 2017 23:11:59 +0000 Received: from sog-mx-3.v43.ch3.sourceforge.com ([172.29.43.193] helo=mx.sourceforge.net) by sfs-ml-4.v29.ch3.sourceforge.com with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.89) (envelope-from <simon@rozman.si>) id 1e23gs-00008P-Pg for openvpn-devel@lists.sourceforge.net; Tue, 10 Oct 2017 23:11:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=DdDkcioBMf5N+fAxkHkFIf3aT6DGVSN2GU4ue6YhTcQ=; b=caOtAqiO1qNzUyIKd/6hdfR2D16U9zBbE1+Y7iOi3QYLHtwHRVqTKK1Pg1C7aLCjTqupeh91P8VTLsKeWIREnKY4BVjjAbNagq8gRvJeL6fGE/+Bs6Wj5vlBayM0fZT8XPdmCtolxI0YAQ5MJjgdhcQjn+8neQt2XF9BHdJKNoE=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=DdDkcioBMf5N+fAxkHkFIf3aT6DGVSN2GU4ue6YhTcQ=; b=hv7tDJi6lMA+GPOoapWJbjGLOZp0aHz9fMtbaxKj1w6DJcnLT8zH9Goomhd95p4aViLidbUbBJ4w4doJaROvtk5W4TV01mz0VZq78X3cDowtrNMPGKqa+C95yQfD1xKqGq4PtaJ1xHeC21FDsmO8D2l4fCzoNpAf4dHSuVaH2u8=; Received-SPF: pass (sog-mx-3.v43.ch3.sourceforge.com: domain of rozman.si designates 89.212.80.145 as permitted sender) client-ip=89.212.80.145; envelope-from=simon@rozman.si; helo=amebis01.amebis.si; Received: from amebis01.amebis.si ([89.212.80.145]) by sog-mx-3.v43.ch3.sourceforge.com with esmtps (TLSv1:AES256-SHA:256) (Exim 4.76) id 1e23gr-0000i7-Mn for openvpn-devel@lists.sourceforge.net; Tue, 10 Oct 2017 23:11:58 +0000 Received: from SR5.amebis.doma (unknown [192.168.100.253]) by amebis01.amebis.si (Postfix) with ESMTP id A266F407BB; Wed, 11 Oct 2017 01:11:33 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=rozman.si; s=default; t=1507677093; bh=yw65itfIycaAKgKCbYyzJxNgzGFAs8ICrDTc+f8cdtY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References; b=mr+Nfha9MZUBC0P8VOBGWqq91DAcwODsZfEg9g1OOaqwmDL747+s8qZgPd27xLySg hb8nPPIqz0k4PWkDlVcTp/ILkwxz0WhOINJ0iRmIU58Ny4/FIv4W4pg+FCwhWhjBCQ 1jiCBrWT1HmB4yHo1kfFuqrbZDq8zvFDLVhGGIIY= From: simon@rozman.si To: openvpn-devel@lists.sourceforge.net Date: Wed, 11 Oct 2017 01:11:29 +0200 Message-Id: <20171010231130.6832-12-simon@rozman.si> X-Mailer: git-send-email 2.9.0.windows.1 In-Reply-To: <20171010231130.6832-1-simon@rozman.si> References: <20171010231130.6832-1-simon@rozman.si> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [89.212.80.145 listed in list.dnswl.org] -1.5 SPF_CHECK_PASS SPF reports sender host as permitted sender for sender-domain -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature 0.1 AWL AWL: Adjusted score from AWL reputation of From: address X-Headers-End: 1e23gr-0000i7-Mn Subject: [Openvpn-devel] [PATCH 12/13] Memory size arithmetic reviewed according to 64-bit MSVC complaints X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: <openvpn-devel.lists.sourceforge.net> List-Unsubscribe: <https://lists.sourceforge.net/lists/options/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=unsubscribe> List-Archive: <http://sourceforge.net/mailarchive/forum.php?forum_name=openvpn-devel> List-Post: <mailto:openvpn-devel@lists.sourceforge.net> List-Help: <mailto:openvpn-devel-request@lists.sourceforge.net?subject=help> List-Subscribe: <https://lists.sourceforge.net/lists/listinfo/openvpn-devel>, <mailto:openvpn-devel-request@lists.sourceforge.net?subject=subscribe> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox |
| Series |
[Openvpn-devel,01/13] snwprintf() => _snwprintf()
|
|
Commit Message
Simon Rozman
Oct. 10, 2017, 12:11 p.m. UTC
From: Simon Rozman <simon@rozman.si>
---
src/openvpnserv/automatic.c | 14 ++++++++------
src/openvpnserv/interactive.c | 12 ++++++------
2 files changed, 14 insertions(+), 12 deletions(-)
Comments
Hi, Some of these changes are of dubious value as the string lengths involved are guaranteed to be small and there is no scope for overflow. And casting only stops the compiler warning, not potential overflow, if any.. As for the offending mixed int/size_t arithmetic, a better option is to just to avoid the arithmetic -- code gets clearer and shorter too: On Tue, Oct 10, 2017 at 7:11 PM, <simon@rozman.si> wrote: > From: Simon Rozman <simon@rozman.si> > > --- > src/openvpnserv/automatic.c | 14 ++++++++------ > src/openvpnserv/interactive.c | 12 ++++++------ > 2 files changed, 14 insertions(+), 12 deletions(-) > > diff --git a/src/openvpnserv/automatic.c b/src/openvpnserv/automatic.c > index 6c39aaa..3e8f6ed 100644 > --- a/src/openvpnserv/automatic.c > +++ b/src/openvpnserv/automatic.c > @@ -130,24 +130,26 @@ close_if_open(HANDLE h) > static bool > match(const WIN32_FIND_DATA *find, LPCTSTR ext) > { - int i; > + size_t i, len_filename, len_ext; > Instead, we can just get rid of these as below if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > { > return false; > } > The rest of this function (extension matching) may be made simpler and hopefully easier to follow as if (*ext == TEXT('\0')) { return false; } /* find the pointer to that last '.' in filename and match ext against the rest */ const char *p = _tcsrchr(find->cFileName, TEXT('.')); return p && p != find->cFileName && !_tcsicmp(p+1, ext); } No _tcslen() and no offending size variables. > - if (!_tcslen(ext)) > + len_ext = _tcslen(ext); > + if (!len_ext) > { > return true; > } > > - i = _tcslen(find->cFileName) - _tcslen(ext) - 1; > - if (i < 1) > + len_filename = _tcslen(find->cFileName); > + if (len_filename < len_ext + 2) > { > return false; > } > > + i = len_filename - len_ext - 1; > return find->cFileName[i] == '.' && !_tcsicmp(find->cFileName + i + > 1, ext); } > And that convoluted original is then gone. > @@ -157,14 +159,14 @@ match(const WIN32_FIND_DATA *find, LPCTSTR ext) > static bool > modext(LPTSTR dest, size_t size, LPCTSTR src, LPCTSTR newext) > { > - int i; > + size_t i; > OK if (size > 0 && (_tcslen(src) + 1) <= size) > { > _tcscpy(dest, src); > dest [size - 1] = TEXT('\0'); > i = _tcslen(dest); > - while (--i >= 0) > + while (i-- > 0) { > if (dest[i] == TEXT('\\')) > { diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c > index 96e0de0..9a4c01a 100644 > --- a/src/openvpnserv/interactive.c > +++ b/src/openvpnserv/interactive.c > @@ -280,7 +280,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, > LPHANDLE events) > _snwprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg); > buf[_countof(buf) - 1] = '\0'; > > - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events); > + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events); Isn't this identical to the original automatic conversion from size_t to DWORD? An overly paranoid assert(_countof(buf) < MAXDWORD/2) may catch an extremely unlikely future coding error, but in what way a cast safer? } > > static VOID > @@ -308,7 +308,7 @@ ReturnError(HANDLE pipe, DWORD error, LPCWSTR func, > DWORD count, LPHANDLE events > L"0x%1!08x!\n%2!s!\n%3!s!", 0, 0, > (LPWSTR) &result, 0, (va_list *) args); > > - WritePipeAsync(pipe, result, wcslen(result) * 2, count, events); > + WritePipeAsync(pipe, result, (DWORD)(wcslen(result) * 2), count, > events); > Same here. > #ifdef UNICODE > MsgToEventLog(MSG_FLAGS_ERROR, result); > #else > @@ -452,10 +452,10 @@ out: > static BOOL > GetStartupData(HANDLE pipe, STARTUP_DATA *sud) > { > - size_t len; > + size_t size, len; > BOOL ret = FALSE; > WCHAR *data = NULL; > - DWORD size, bytes, read; > + DWORD bytes, read; > OK > > bytes = PeekNamedPipeAsync(pipe, 1, &exit_event); > if (bytes == 0) > @@ -1163,7 +1163,7 @@ netsh_dns_cmd(const wchar_t *action, const wchar_t > *proto, const wchar_t *if_nam > const wchar_t *fmt = L"netsh interface %s %s dns \"%s\" %s"; > > /* max cmdline length in wchars -- include room for worst case and > some */ > - int ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + 1; > + size_t ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + > 1; > OK > wchar_t *cmdline = malloc(ncmdline*sizeof(wchar_t)); > if (!cmdline) > { > @@ -1683,7 +1683,7 @@ RunOpenvpn(LPVOID p) > { > DWORD written; > WideCharToMultiByte(CP_UTF8, 0, sud.std_input, -1, input, > input_size, NULL, NULL); > - WriteFile(stdin_write, input, strlen(input), &written, NULL); > + WriteFile(stdin_write, input, (DWORD)strlen(input), &written, > NULL); > As above.. > free(input); > } > > Thanks, Selva <div dir="ltr">Hi,<br><div class="gmail_extra"><br></div><div class="gmail_extra">Some of these changes are of dubious value as the string lengths</div><div class="gmail_extra">involved are guaranteed to be small and there is no scope</div><div class="gmail_extra">for overflow. And casting only stops the compiler warning, not potential</div><div class="gmail_extra">overflow, if any..</div><div class="gmail_extra"><br></div><div class="gmail_extra">As for the offending mixed int/size_t arithmetic, a better option is</div><div class="gmail_extra">to just to avoid the arithmetic -- code gets clearer and shorter too:</div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_quote">On Tue, Oct 10, 2017 at 7:11 PM, <span dir="ltr"><<a href="mailto:simon@rozman.si" target="_blank">simon@rozman.si</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">From: Simon Rozman <<a href="mailto:simon@rozman.si" target="_blank">simon@rozman.si</a>><br> <br> ---<br> src/openvpnserv/automatic.c | 14 ++++++++------<br> src/openvpnserv/interactive.c | 12 ++++++------<br> 2 files changed, 14 insertions(+), 12 deletions(-)<br> <br> diff --git a/src/openvpnserv/automatic.c b/src/openvpnserv/automatic.c<br> index 6c39aaa..3e8f6ed 100644<br> --- a/src/openvpnserv/automatic.c<br> +++ b/src/openvpnserv/automatic.c<br> @@ -130,24 +130,26 @@ close_if_open(HANDLE h)<br> static bool<br> match(const WIN32_FIND_DATA *find, LPCTSTR ext)<br> { </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> - int i;<br> + size_t i, len_filename, len_ext;<br></blockquote><div><br></div><div>Instead, we can just get rid of these as below</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)<br> {<br> return false;<br> }<br></blockquote><div><br></div><div>The rest of this function (extension matching) may be made simpler</div><div>and hopefully easier to follow as</div><div><br></div><div> if (*ext == TEXT('\0'))</div><div> {</div><div> return false;</div><div> }</div><div><br></div><div> /* find the pointer to that last '.' in filename and match ext against the rest */</div><div><br></div><div> const char *p = _tcsrchr(find->cFileName, TEXT('.'));</div><div> return p && p != find->cFileName && !_tcsicmp(p+1, ext);</div><div>}</div><div><br></div><div>No _tcslen() and no offending size variables.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br> - if (!_tcslen(ext))<br> + len_ext = _tcslen(ext);<br> + if (!len_ext)<br> {<br> return true;<br> }<br> <br> - i = _tcslen(find->cFileName) - _tcslen(ext) - 1;<br> - if (i < 1)<br> + len_filename = _tcslen(find->cFileName);<br> + if (len_filename < len_ext + 2)<br> {<br> return false;<br> }<br> <br> + i = len_filename - len_ext - 1;<br> return find->cFileName[i] == '.' && !_tcsicmp(find->cFileName + i + 1, ext);</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> }<br></blockquote><div><br></div><div>And that convoluted original is then gone.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br> @@ -157,14 +159,14 @@ match(const WIN32_FIND_DATA *find, LPCTSTR ext)<br> static bool<br> modext(LPTSTR dest, size_t size, LPCTSTR src, LPCTSTR newext)<br> {<br> - int i;<br> + size_t i;<br></blockquote><div><br></div><div>OK</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> if (size > 0 && (_tcslen(src) + 1) <= size)<br> {<br> _tcscpy(dest, src);<br> dest [size - 1] = TEXT('\0');<br> i = _tcslen(dest);<br> - while (--i >= 0)<br> + while (i-- > 0) </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> {<br> if (dest[i] == TEXT('\\'))<br> {</blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> diff --git a/src/openvpnserv/interactive.<wbr>c b/src/openvpnserv/interactive.<wbr>c<br> index 96e0de0..9a4c01a 100644<br> --- a/src/openvpnserv/interactive.<wbr>c<br> +++ b/src/openvpnserv/interactive.<wbr>c<br> @@ -280,7 +280,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events)<br> _snwprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg);<br> buf[_countof(buf) - 1] = '\0';<br> <br> - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events);<br> + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events);</blockquote><div><br></div><div>Isn't this identical to the original automatic conversion from size_t to DWORD?</div><div>An overly paranoid assert(_countof(buf) < MAXDWORD/2) may catch an extremely</div><div>unlikely future coding error, but in what way a cast safer?</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> }<br> <br> static VOID<br> @@ -308,7 +308,7 @@ ReturnError(HANDLE pipe, DWORD error, LPCWSTR func, DWORD count, LPHANDLE events<br> L"0x%1!08x!\n%2!s!\n%3!s!", 0, 0,<br> (LPWSTR) &result, 0, (va_list *) args);<br> <br> - WritePipeAsync(pipe, result, wcslen(result) * 2, count, events);<br> + WritePipeAsync(pipe, result, (DWORD)(wcslen(result) * 2), count, events);<br></blockquote><div><br></div><div>Same here.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> #ifdef UNICODE<br> MsgToEventLog(MSG_FLAGS_ERROR<wbr>, result);<br> #else<br> @@ -452,10 +452,10 @@ out:<br> static BOOL<br> GetStartupData(HANDLE pipe, STARTUP_DATA *sud)<br> {<br> - size_t len;<br> + size_t size, len;<br> BOOL ret = FALSE;<br> WCHAR *data = NULL;<br> - DWORD size, bytes, read;<br> + DWORD bytes, read;<br></blockquote><div><br></div><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> <br> bytes = PeekNamedPipeAsync(pipe, 1, &exit_event);<br> if (bytes == 0)<br> @@ -1163,7 +1163,7 @@ netsh_dns_cmd(const wchar_t *action, const wchar_t *proto, const wchar_t *if_nam<br> const wchar_t *fmt = L"netsh interface %s %s dns \"%s\" %s";<br> <br> /* max cmdline length in wchars -- include room for worst case and some */<br> - int ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + 1;<br> + size_t ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + 1;<br></blockquote><div><br></div><div>OK</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> wchar_t *cmdline = malloc(ncmdline*sizeof(wchar_t<wbr>));<br> if (!cmdline)<br> {<br> @@ -1683,7 +1683,7 @@ RunOpenvpn(LPVOID p)<br> {<br> DWORD written;<br> WideCharToMultiByte(CP_UTF8, 0, sud.std_input, -1, input, input_size, NULL, NULL);<br> - WriteFile(stdin_write, input, strlen(input), &written, NULL);<br> + WriteFile(stdin_write, input, (DWORD)strlen(input), &written, NULL);<br></blockquote><div><br></div><div>As above..</div><div> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"> free(input);<br> }<br> <span class="m_2792988390416823077m_5558159874359295240m_8470299133570871556m_8856265837151638397m_6434295266320317594m_1582231105454850336gmail-m_-6650795910552092661HOEnZb"><font color="#888888"><br></font></span></blockquote><div><br></div><div>Thanks,</div><div><br></div><div>Selva</div></div></div></div> ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Hi, > Some of these changes are of dubious value as the string lengths involved > are guaranteed to be small and there is no scope for overflow. And casting > only stops the compiler warning, not potential overflow, if any.. Exactly. Where there's no scope for an overflow and compiler is too dumb to notice that, why not "teaching" it a bit? > As for the offending mixed int/size_t arithmetic, a better option is to just > to > avoid the arithmetic -- code gets clearer and shorter too: Though, I see no problem with size_t/ptrdiff_t arithmetic when implemented properly. O:-) > const char *p = _tcsrchr(find->cFileName, TEXT('.')); > return p && p != find->cFileName && !_tcsicmp(p+1, ext); > > No _tcslen() and no offending size variables. > And that convoluted original is then gone. The resulting code would look a lot nicer indeed. Stay tuned for v2 of this patch. > - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events); > + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events); > > Isn't this identical to the original automatic conversion from size_t to > DWORD? > An overly paranoid assert(_countof(buf) < MAXDWORD/2) may catch an > extremely > unlikely future coding error, but in what way a cast safer? I didn't say it is safer. The buf is WCHAR[33] on the stack. There is a safety zero termination a line before the WritePipeAsync(). Therefore, wcslen(buf) will always return between 0 and 32. Times two (if I was coding this, I'd use *sizeof(WCHAR) instead of 2, to make a clear statement, what is multiplication by 2 about), making it between 0 and 64. Unfortunately, result is size_t where Win32 API expects a DWORD. This will always produce a false compiler warning. I muted it by an explicit cast, so we can focus on more important warnings. > - WritePipeAsync(pipe, result, wcslen(result) * 2, count, events); > + WritePipeAsync(pipe, result, (DWORD)(wcslen(result) * 2), count, > events); > > Same here. The `result` is a Windows error message. Do you really expect Windows error messages will ever grow beyond 2G? (2G TCHARs*sizeof(TCHAR) = 4GB > MAXDWORD) > - WriteFile(stdin_write, input, strlen(input), &written, NULL); > + WriteFile(stdin_write, input, (DWORD)strlen(input), &written, > NULL); > > As above.. Again, you should observe the broader context of the code. The `input` buffer is allocated on the heap and its size was calculated as DWORD. (Although, we could research here, how the size calculation behaves for strings longer than 4G.) Anyway, its size will always be <=MAXDWORD. According to the given parameters, the WideCharToMultiByte() MUST zero-terminate the `input` buffer. Therefore, strlen(input) is guaranteed to be <MAXDWORD. Therefore, casting from size_t to DWORD is perfectly safe. Best regards, Simon ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
Hi, > > const char *p = _tcsrchr(find->cFileName, TEXT('.')); > > return p && p != find->cFileName && !_tcsicmp(p+1, ext); > > > > No _tcslen() and no offending size variables. > > And that convoluted original is then gone. > > The resulting code would look a lot nicer indeed. Stay tuned for v2 of > this patch. > > Yes that was the point -- not that we cant do arithmetic, but we need not and in the end the code could be cleaner... > > > - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events); > > + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events); > > > > Isn't this identical to the original automatic conversion from size_t to > > DWORD? > > An overly paranoid assert(_countof(buf) < MAXDWORD/2) may catch an > > extremely > > unlikely future coding error, but in what way a cast safer? > > I didn't say it is safer. The buf is WCHAR[33] on the stack. There is a > safety zero termination a line before the WritePipeAsync(). Therefore, > wcslen(buf) will always return between 0 and 32. Times two (if I was coding > this, I'd use *sizeof(WCHAR) instead of 2, to make a clear statement, what > is multiplication by 2 about), making it between 0 and 64. Unfortunately, > result is size_t where Win32 API expects a DWORD. > > This will always produce a false compiler warning. I muted it by an > explicit cast, so we can focus on more important warnings. Agreed, reducing compiler warnings is a useful side effect and we know the type coercion here is safe. cheers, Selva <div dir="ltr">Hi,<div><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""> > const char *p = _tcsrchr(find->cFileName, TEXT('.'));<br> > return p && p != find->cFileName && !_tcsicmp(p+1, ext);<br> ><br> > No _tcslen() and no offending size variables.<br> </span><span class="">> And that convoluted original is then gone.<br> <br> </span>The resulting code would look a lot nicer indeed. Stay tuned for v2 of this patch.<br> <span class=""><br></span></blockquote><div><br></div><div>Yes that was the point -- not that we cant do arithmetic, but we need not</div><div>and in the end the code could be cleaner...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""> <br> > - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events);<br> > + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events);<br> ><br> > Isn't this identical to the original automatic conversion from size_t to<br> > DWORD?<br> > An overly paranoid assert(_countof(buf) < MAXDWORD/2) may catch an<br> > extremely<br> > unlikely future coding error, but in what way a cast safer?<br> <br> </span>I didn't say it is safer. The buf is WCHAR[33] on the stack. There is a safety zero termination a line before the WritePipeAsync(). Therefore, wcslen(buf) will always return between 0 and 32. Times two (if I was coding this, I'd use *sizeof(WCHAR) instead of 2, to make a clear statement, what is multiplication by 2 about), making it between 0 and 64. Unfortunately, result is size_t where Win32 API expects a DWORD.<br> <br> This will always produce a false compiler warning. I muted it by an explicit cast, so we can focus on more important warnings.</blockquote><div><br></div><div>Agreed, reducing compiler warnings is a useful side effect and we know the</div><div>type coercion here is safe.</div><div><br></div><div>cheers,</div><div><br></div><div>Selva</div></div></div></div></div> ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot
diff --git a/src/openvpnserv/automatic.c b/src/openvpnserv/automatic.c index 6c39aaa..3e8f6ed 100644 --- a/src/openvpnserv/automatic.c +++ b/src/openvpnserv/automatic.c @@ -130,24 +130,26 @@ close_if_open(HANDLE h) static bool match(const WIN32_FIND_DATA *find, LPCTSTR ext) { - int i; + size_t i, len_filename, len_ext; if (find->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { return false; } - if (!_tcslen(ext)) + len_ext = _tcslen(ext); + if (!len_ext) { return true; } - i = _tcslen(find->cFileName) - _tcslen(ext) - 1; - if (i < 1) + len_filename = _tcslen(find->cFileName); + if (len_filename < len_ext + 2) { return false; } + i = len_filename - len_ext - 1; return find->cFileName[i] == '.' && !_tcsicmp(find->cFileName + i + 1, ext); } @@ -157,14 +159,14 @@ match(const WIN32_FIND_DATA *find, LPCTSTR ext) static bool modext(LPTSTR dest, size_t size, LPCTSTR src, LPCTSTR newext) { - int i; + size_t i; if (size > 0 && (_tcslen(src) + 1) <= size) { _tcscpy(dest, src); dest [size - 1] = TEXT('\0'); i = _tcslen(dest); - while (--i >= 0) + while (i-- > 0) { if (dest[i] == TEXT('\\')) { diff --git a/src/openvpnserv/interactive.c b/src/openvpnserv/interactive.c index 96e0de0..9a4c01a 100644 --- a/src/openvpnserv/interactive.c +++ b/src/openvpnserv/interactive.c @@ -280,7 +280,7 @@ ReturnProcessId(HANDLE pipe, DWORD pid, DWORD count, LPHANDLE events) _snwprintf(buf, _countof(buf), L"0x%08x\n0x%08x\n%s", 0, pid, msg); buf[_countof(buf) - 1] = '\0'; - WritePipeAsync(pipe, buf, wcslen(buf) * 2, count, events); + WritePipeAsync(pipe, buf, (DWORD)(wcslen(buf) * 2), count, events); } static VOID @@ -308,7 +308,7 @@ ReturnError(HANDLE pipe, DWORD error, LPCWSTR func, DWORD count, LPHANDLE events L"0x%1!08x!\n%2!s!\n%3!s!", 0, 0, (LPWSTR) &result, 0, (va_list *) args); - WritePipeAsync(pipe, result, wcslen(result) * 2, count, events); + WritePipeAsync(pipe, result, (DWORD)(wcslen(result) * 2), count, events); #ifdef UNICODE MsgToEventLog(MSG_FLAGS_ERROR, result); #else @@ -452,10 +452,10 @@ out: static BOOL GetStartupData(HANDLE pipe, STARTUP_DATA *sud) { - size_t len; + size_t size, len; BOOL ret = FALSE; WCHAR *data = NULL; - DWORD size, bytes, read; + DWORD bytes, read; bytes = PeekNamedPipeAsync(pipe, 1, &exit_event); if (bytes == 0) @@ -1163,7 +1163,7 @@ netsh_dns_cmd(const wchar_t *action, const wchar_t *proto, const wchar_t *if_nam const wchar_t *fmt = L"netsh interface %s %s dns \"%s\" %s"; /* max cmdline length in wchars -- include room for worst case and some */ - int ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + 1; + size_t ncmdline = wcslen(fmt) + wcslen(if_name) + wcslen(addr) + 32 + 1; wchar_t *cmdline = malloc(ncmdline*sizeof(wchar_t)); if (!cmdline) { @@ -1683,7 +1683,7 @@ RunOpenvpn(LPVOID p) { DWORD written; WideCharToMultiByte(CP_UTF8, 0, sud.std_input, -1, input, input_size, NULL, NULL); - WriteFile(stdin_write, input, strlen(input), &written, NULL); + WriteFile(stdin_write, input, (DWORD)strlen(input), &written, NULL); free(input); }