Fix -Wformat-length warning in time/tst-strptime2.c

Message ID 1478116026.2891.53.camel@caviumnetworks.com
State New, archived
Headers

Commit Message

Steve Ellcey Nov. 2, 2016, 7:47 p.m. UTC
  Here is an updated version of my earlier patch, I hadn't tested the
original one with an old GCC so I hadn't realized I needed to put the
DIAG_IGNORE_NEEDS_COMMENT in an ifdef.  This version was tested with
GCC 5.4 and GCC 7.0 with no regressions.

I also double checked that the test program is deliberately using
snprintf to truncate the output so changing the test by increasing the
buffer size and avoiding the warnings that way would change what was
being tested.

OK to checkin?


2016-11-02  Steve Ellcey  <sellcey@caviumnetworks.com>

	* time/tst-strptime2.c: Ignore -Wformat-length warning.




+#endif
 
   if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
     {
  

Comments

Carlos O'Donell Nov. 2, 2016, 8:55 p.m. UTC | #1
On 11/02/2016 03:47 PM, Steve Ellcey wrote:
> Here is an updated version of my earlier patch, I hadn't tested the
> original one with an old GCC so I hadn't realized I needed to put the
> DIAG_IGNORE_NEEDS_COMMENT in an ifdef.  This version was tested with
> GCC 5.4 and GCC 7.0 with no regressions.
> 
> I also double checked that the test program is deliberately using
> snprintf to truncate the output so changing the test by increasing the
> buffer size and avoiding the warnings that way would change what was
> being tested.
> 
> OK to checkin?

LGTM.

c.
  

Patch

diff --git a/time/tst-strptime2.c b/time/tst-strptime2.c
index 7fe7350..53b366b 100644
--- a/time/tst-strptime2.c
+++ b/time/tst-strptime2.c
@@ -4,6 +4,7 @@ 
 #include <stdbool.h>
 #include <stdio.h>
 #include <time.h>
+#include <libc-internal.h>
 
 /* Dummy string is used to match strptime's %s specifier.  */
 
@@ -67,10 +68,20 @@  mkbuf (char *buf, bool neg, bool colon, unsigned
int hhmm, s
ize_t ndigits)
   long int expect = LONG_MAX;
 
   i = sprintf (buf, "%s %c", dummy_string, sign);
+#if __GNUC_PREREQ (7, 0)
+  /* GCC issues a warning when it thinks the snprintf buffer may be
too short.
+     This test is explicitly using short buffers to force snprintf to
truncate
+     the output so we ignore the warnings.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-length");
+#endif
   if (colon)
     snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm);
   else
     snprintf (buf + i, ndigits + 1, "%04u", hhmm);
+#if __GNUC_PREREQ (7, 0)
+  DIAG_POP_NEEDS_COMMENT;