]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
who: avoid new warning from upcoming gcc-7
authorJim Meyering <meyering@fb.com>
Thu, 22 Sep 2016 14:56:15 +0000 (07:56 -0700)
committerJim Meyering <meyering@fb.com>
Thu, 22 Sep 2016 17:17:07 +0000 (10:17 -0700)
* src/who.c (idle_string): This function would fail to compile
with -Werror and today's built-from-git gcc due to this warning:
src/who.c: In function 'print_user':
src/who.c:201:36: error: may write format character ':' at offset 4 \
  past the end of the destination [-Werror=format-length=]
           sprintf (idle_hhmm, "%02d:%02d",
                                    ^~~~~
The fix is to use an assertion to inform gcc of the existing
invariant that guarantees the number of hours is less than 24.

src/who.c

index f56c71833210375e6b4a10ab6e02b84c7789171a..d5f5732558c2f41283f4803db9f9f5a8a412357c 100644 (file)
--- a/src/who.c
+++ b/src/who.c
@@ -26,6 +26,7 @@
 #include <config.h>
 #include <getopt.h>
 #include <stdio.h>
+#include <assert.h>
 
 #include <sys/types.h>
 #include "system.h"
@@ -198,6 +199,9 @@ idle_string (time_t when, time_t boottime)
       else
         {
           static char idle_hhmm[IDLESTR_LEN];
+          /* FIXME-in-2018: see if this assert is still required in order
+             to suppress gcc's unwarranted -Wformat-length= warning.  */
+          assert (seconds_idle / (60 * 60) < 24);
           sprintf (idle_hhmm, "%02d:%02d",
                    seconds_idle / (60 * 60),
                    (seconds_idle % (60 * 60)) / 60);