From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Ken Brown Newsgroups: gmane.emacs.bugs Subject: bug#32189: 27.0.50; GCC 7 warning due to -Wformat-truncation=2 Date: Wed, 18 Jul 2018 15:42:55 -0400 Message-ID: References: <031d18cf-e661-6dea-8882-38d75924d0d4@cornell.edu> <83601c4m37.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: blaine.gmane.org 1531942926 8797 195.159.176.226 (18 Jul 2018 19:42:06 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 18 Jul 2018 19:42:06 +0000 (UTC) User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 Cc: 32189@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Wed Jul 18 21:42:01 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 1ffsKn-0002BD-LM for geb-bug-gnu-emacs@m.gmane.org; Wed, 18 Jul 2018 21:42:01 +0200 Original-Received: from localhost ([::1]:38101 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffsMu-0003A4-L7 for geb-bug-gnu-emacs@m.gmane.org; Wed, 18 Jul 2018 15:44:12 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:48045) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ffsMo-00039l-34 for bug-gnu-emacs@gnu.org; Wed, 18 Jul 2018 15:44:07 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ffsMk-0007Eo-2R for bug-gnu-emacs@gnu.org; Wed, 18 Jul 2018 15:44:06 -0400 Original-Received: from debbugs.gnu.org ([208.118.235.43]:42189) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1ffsMj-0007Ef-Tf for bug-gnu-emacs@gnu.org; Wed, 18 Jul 2018 15:44:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1ffsMj-0005RX-Mm for bug-gnu-emacs@gnu.org; Wed, 18 Jul 2018 15:44:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ken Brown Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Wed, 18 Jul 2018 19:44:01 +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.153194298720851 (code B ref 32189); Wed, 18 Jul 2018 19:44:01 +0000 Original-Received: (at 32189) by debbugs.gnu.org; 18 Jul 2018 19:43:07 +0000 Original-Received: from localhost ([127.0.0.1]:47207 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ffsLr-0005QE-An for submit@debbugs.gnu.org; Wed, 18 Jul 2018 15:43:07 -0400 Original-Received: from limerock02.mail.cornell.edu ([128.84.13.242]:34536) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1ffsLn-0005Pj-Bb for 32189@debbugs.gnu.org; Wed, 18 Jul 2018 15:43:06 -0400 X-CornellRouted: This message has been Routed already. Original-Received: from authusersmtp.mail.cornell.edu (granite4.serverfarm.cornell.edu [10.16.197.9]) by limerock02.mail.cornell.edu (8.14.4/8.14.4_cu) with ESMTP id w6IJgvWT019047; Wed, 18 Jul 2018 15:42:57 -0400 Original-Received: from [192.168.0.4] (mta-68-175-129-7.twcny.rr.com [68.175.129.7] (may be forged)) (authenticated bits=0) by authusersmtp.mail.cornell.edu (8.14.4/8.12.10) with ESMTP id w6IJgtao031035 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES128-SHA bits=128 verify=NOT); Wed, 18 Jul 2018 15:42:56 -0400 In-Reply-To: <83601c4m37.fsf@gnu.org> Content-Language: en-US X-PMX-Cornell-Gauge: Gauge=XXXXX X-PMX-CORNELL-AUTH-RESULTS: dkim-out=none; 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:148646 Archived-At: On 7/18/2018 11:09 AM, Eli Zaretskii wrote: >> From: Ken Brown >> Date: Tue, 17 Jul 2018 15:26:34 -0400 >> >> I'm getting the following warning when building the master branch on >> Cygwin with GCC 7.3: >> >> In file included from /usr/include/stdio.h:800:0, >> from ../lib/stdio.h:43, >> from ../../master/src/w32cygwinx.c:22: >> ../../master/src/w32cygwinx.c: In function ‘Fw32_battery_status’: >> ../../master/src/w32cygwinx.c:116:26: warning: ‘%3.1f’ directive output >> may be truncated writing between 3 and 312 bytes into a region of size >> 16 [-Wformat-truncation=] >> snprintf (buffer, 16, "%3.1f", h); >> ^ >> ../../master/src/w32cygwinx.c:116:4: note: ‘__builtin_snprintf’ output >> between 4 and 313 bytes into a destination of size 16 >> snprintf (buffer, 16, "%3.1f", h); >> ^ > > Do we really need to use -Wformat-truncation? Is it a useful warning > switch? The above sounds like useless noise, because the code > explicitly _asks_ for truncation. What do people think about this? Moreover, the warning isn't very smart; see below. >> The attached patch avoids the warning. Is this a reasonable fix, or is >> there a better way? > > I think if we keep the switch, a better fix is to do this: > > snprintf (buffer, 16, "%ld", h % 1000000); > > m = seconds_left / 60; > snprintf (buffer, 16, "%ld", m % 20000); > > etc., you get the point. This doesn't work with GCC 7. (Maybe it would work with GCC 8; the release notes say that it is better at avoiding false positives.) For integer specifiers like "%ld", the only thing I've found that works without enlarging the buffer is to cast the argument to a smaller integer type. For float specifiers like "%3.1f", even using a small type doesn't seem to work. For example: $ cat test.c #include int main () { char buffer[16]; short a; snprintf (buffer, 16, "%3.1f", a); } $ gcc -Wformat-truncation=2 test.c test.c: In function ‘main’: test.c:8:26: warning: ‘%3.1f’ directive output may be truncated writing between 3 and 312 bytes into a region of size 16 [-Wformat-truncation=] snprintf (buffer, 16, "%3.1f", a); ^~~~~ test.c:8:3: note: ‘snprintf’ output between 4 and 313 bytes into a destination of size 16 snprintf (buffer, 16, "%3.1f", a); ^~~~~~~~~~~~~~~~~~~~~ Ken