From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.bugs Subject: bug#32189: 27.0.50; GCC 7 warning due to -Wformat-truncation=2 Date: Thu, 19 Jul 2018 16:19:38 -0700 Organization: UCLA Computer Science Department Message-ID: <6642c8ac-55ff-ca2b-0141-a26b69d082ad@cs.ucla.edu> References: <031d18cf-e661-6dea-8882-38d75924d0d4@cornell.edu> <83601c4m37.fsf@gnu.org> <83in5b2w1q.fsf@gnu.org> <83d0vj2tu2.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------83BBFA3F2E3E1E5837A61833" X-Trace: blaine.gmane.org 1532042289 12452 195.159.176.226 (19 Jul 2018 23:18:09 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 19 Jul 2018 23:18:09 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cc: p.stephani2@gmail.com, 32189@debbugs.gnu.org To: Eli Zaretskii , Ken Brown Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Fri Jul 20 01:18:05 2018 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fgIBQ-00038v-FY for geb-bug-gnu-emacs@m.gmane.org; Fri, 20 Jul 2018 01:18:04 +0200 Original-Received: from localhost ([::1]:45747 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fgIDW-0007Kd-U8 for geb-bug-gnu-emacs@m.gmane.org; Thu, 19 Jul 2018 19:20:14 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fgIDP-0007J3-RE for bug-gnu-emacs@gnu.org; Thu, 19 Jul 2018 19:20:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fgIDK-0008SC-Sk for bug-gnu-emacs@gnu.org; Thu, 19 Jul 2018 19:20:07 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:43619) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fgIDK-0008S6-OF for bug-gnu-emacs@gnu.org; Thu, 19 Jul 2018 19:20:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1fgIDK-0004WQ-Bw for bug-gnu-emacs@gnu.org; Thu, 19 Jul 2018 19:20:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Paul Eggert Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 19 Jul 2018 23:20:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 32189 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 32189-submit@debbugs.gnu.org id=B32189.153204239117360 (code B ref 32189); Thu, 19 Jul 2018 23:20:02 +0000 Original-Received: (at 32189) by debbugs.gnu.org; 19 Jul 2018 23:19:51 +0000 Original-Received: from localhost ([127.0.0.1]:48637 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fgID8-0004Vu-SG for submit@debbugs.gnu.org; Thu, 19 Jul 2018 19:19:51 -0400 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:42520) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fgID5-0004Ve-8r for 32189@debbugs.gnu.org; Thu, 19 Jul 2018 19:19:49 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 89187160885; Thu, 19 Jul 2018 16:19:41 -0700 (PDT) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id 5nFn-UA6TWoH; Thu, 19 Jul 2018 16:19:40 -0700 (PDT) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 3D55C160CB9; Thu, 19 Jul 2018 16:19:40 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id Nhd9_mKcAAiw; Thu, 19 Jul 2018 16:19:40 -0700 (PDT) Original-Received: from [192.168.1.9] (unknown [47.154.30.119]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id 93C01160885; Thu, 19 Jul 2018 16:19:39 -0700 (PDT) Openpgp: preference=signencrypt Autocrypt: addr=eggert@cs.ucla.edu; prefer-encrypt=mutual; keydata= xsFNBEyAcmQBEADAAyH2xoTu7ppG5D3a8FMZEon74dCvc4+q1XA2J2tBy2pwaTqfhpxxdGA9 Jj50UJ3PD4bSUEgN8tLZ0san47l5XTAFLi2456ciSl5m8sKaHlGdt9XmAAtmXqeZVIYX/UFS 96fDzf4xhEmm/y7LbYEPQdUdxu47xA5KhTYp5bltF3WYDz1Ygd7gx07Auwp7iw7eNvnoDTAl KAl8KYDZzbDNCQGEbpY3efZIvPdeI+FWQN4W+kghy+P6au6PrIIhYraeua7XDdb2LS1en3Ss mE3QjqfRqI/A2ue8JMwsvXe/WK38Ezs6x74iTaqI3AFH6ilAhDqpMnd/msSESNFt76DiO1ZK QMr9amVPknjfPmJISqdhgB1DlEdw34sROf6V8mZw0xfqT6PKE46LcFefzs0kbg4GORf8vjG2 Sf1tk5eU8MBiyN/bZ03bKNjNYMpODDQQwuP84kYLkX2wBxxMAhBxwbDVZudzxDZJ1C2VXujC OJVxq2kljBM9ETYuUGqd75AW2LXrLw6+MuIsHFAYAgRr7+KcwDgBAfwhPBYX34nSSiHlmLC+ KaHLeCLF5ZI2vKm3HEeCTtlOg7xZEONgwzL+fdKo+D6SoC8RRxJKs8a3sVfI4t6CnrQzvJbB n6gxdgCu5i29J1QCYrCYvql2UyFPAK+do99/1jOXT4m2836j1wARAQABzSBQYXVsIEVnZ2Vy dCA8ZWdnZXJ0QGNzLnVjbGEuZWR1PsLBfgQTAQIAKAUCTIByZAIbAwUJEswDAAYLCQgHAwIG FQgCCQoLBBYCAwECH In-Reply-To: <83d0vj2tu2.fsf@gnu.org> Content-Language: en-US X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 208.118.235.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:148694 Archived-At: This is a multi-part message in MIME format. --------------83BBFA3F2E3E1E5837A61833 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Eli Zaretskii wrote: > Thanks, this LGTM, but I'd like to hear Paul's opinion about this > warning before we push (since this should probably go to the emacs-26 > branch?). Let's continue using -Wformat-truncation=3D2 since I recall it finding re= al bugs=20 in Emacs in the past, and there's a better fix for this particular proble= m. Some background. Generally I don't worry too much about warnings from old= er=20 compilers, since one can just compile with --disable-gcc-warnings if the=20 compiler is too old. It's better to use the latest GCC to find bugs, and = not=20 worry about pacifying older GCCs that are somewhat flaky in this area (as= =20 pacifying them would be a neverending project with little benefit). Two comments for this particular case. First, Emacs generally prefers spr= intf to=20 snprintf, because truncation (which is what the latter does) is often jus= t as=20 serious a bug as buffer overflow (which is what the former does), and the= GNU=20 coding style is to avoid both bugs in which case sprintf is generally sim= pler=20 and easier to use. I realize this goes against the common wisdom that snp= rintf=20 is "safer" than sprintf, but the common wisdom is typically wrong for=20 high-quality code. In the few places where Emacs does use snprintf, it ei= ther=20 checks for truncated output and reports an error if so (which is lame, bu= t at=20 least there's an error check), or truncation is expected and is OK. Second, the code in question uses snprintf followed by build_string, and = can be=20 simplified by using vformat_string instead. That would avoid the warnings= and=20 should make the code more reliable. Something like the attached patch, sa= y. I=20 haven't tested it since I don't use MS-Windows. Let's use something like = this=20 rather than shutting off the warnings. --------------83BBFA3F2E3E1E5837A61833 Content-Type: text/x-patch; name="0001-Simplify-w32cygwinx.c-and-pacify-GCC-Bug-32189.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename*0="0001-Simplify-w32cygwinx.c-and-pacify-GCC-Bug-32189.patch" =46rom 95ed210f17a7970764fb670e8b5d41b3d2ff3084 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Thu, 19 Jul 2018 16:12:17 -0700 Subject: [PATCH] Simplify w32cygwinx.c and pacify GCC (Bug#32189) * src/w32cygwinx.c (format_string): New function. (Fw32_battery_status): Use it. --- src/w32cygwinx.c | 37 ++++++++++++++++--------------------- 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/w32cygwinx.c b/src/w32cygwinx.c index 8d3ae16..044e1fc 100644 --- a/src/w32cygwinx.c +++ b/src/w32cygwinx.c @@ -24,6 +24,16 @@ along with GNU Emacs. If not, see . */ #include "lisp.h" #include "w32common.h" =20 +static Lisp_Object +format_string (char const *format, ...) +{ + va_list args; + va_start (args, format); + Lisp_Object str =3D vformat_string (format, args); + va_end (args); + return str; +} + DEFUN ("w32-battery-status", Fw32_battery_status, Sw32_battery_status, 0= , 0, 0, doc: /* Get power status information from Windows system. =20 @@ -92,32 +102,17 @@ The following %-sequences are provided: if (system_status.BatteryLifePercent > 100) load_percentage =3D build_string ("N/A"); else - { - char buffer[16]; - snprintf (buffer, 16, "%d", system_status.BatteryLifePercent); - load_percentage =3D build_string (buffer); - } + load_percentage =3D format_string ("%d", system_status.BatteryLifePerce= nt); =20 if (seconds_left < 0) seconds =3D minutes =3D hours =3D remain =3D build_string ("N/A"); else { - long m; - double h; - char buffer[16]; - snprintf (buffer, 16, "%ld", seconds_left); - seconds =3D build_string (buffer); - - m =3D seconds_left / 60; - snprintf (buffer, 16, "%ld", m); - minutes =3D build_string (buffer); - - h =3D seconds_left / 3600.0; - snprintf (buffer, 16, "%3.1f", h); - hours =3D build_string (buffer); - - snprintf (buffer, 16, "%ld:%02ld", m / 60, m % 60); - remain =3D build_string (buffer); + long m =3D seconds_left / 60; + seconds =3D format_string ("%ld", seconds_left); + minutes =3D format_string ("%ld", m); + hours =3D format_string ("%3.1f", seconds_left / 3600.0); + remain =3D format_string ("%ld:%02ld", m / 60, m % 60); } =20 status =3D listn (CONSTYPE_HEAP, 8, --=20 2.7.4 --------------83BBFA3F2E3E1E5837A61833--