public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Fix -Wformat-length warning in time/tst-strptime2.c
@ 2016-10-24 16:28 Steve Ellcey
  2016-11-02 19:47 ` Steve Ellcey
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Ellcey @ 2016-10-24 16:28 UTC (permalink / raw)
  To: libc-alpha

This is the third of three patches to deal with the new -Wformat-length
warning in GCC 7.0.  Changing the size of the buffer looked like it
might interfer with what the test was trying to check so I added a
DIAG_IGNORE macro for this test.

Steve Ellcey
sellcey@caviumnetworks.com


2016-10-24  Steve Ellcey  <sellcey@caviumnetworks.com>

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


diff --git a/time/tst-strptime2.c b/time/tst-strptime2.c
index 7fe7350..2f411a4 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,16 @@ 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);
+  /* GCC cannot be certain that the buffer is long enough so it issues a
+     warning.  We know that hhmm is never more than 4 digits so we can ignore
+     the warning.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (7.0, "-Wformat-length");
   if (colon)
     snprintf (buf + i, ndigits + 2, "%02u:%02u", hh, mm);
   else
     snprintf (buf + i, ndigits + 1, "%04u", hhmm);
+  DIAG_POP_NEEDS_COMMENT;
 
   if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
     {

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix -Wformat-length warning in time/tst-strptime2.c
  2016-10-24 16:28 [PATCH] Fix -Wformat-length warning in time/tst-strptime2.c Steve Ellcey
@ 2016-11-02 19:47 ` Steve Ellcey
  2016-11-02 20:55   ` Carlos O'Donell
  0 siblings, 1 reply; 3+ messages in thread
From: Steve Ellcey @ 2016-11-02 19:47 UTC (permalink / raw)
  To: libc-alpha

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.




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;
+#endif
 
   if (mm <= mm_max && (ndigits == 2 || ndigits == 4))
     {


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix -Wformat-length warning in time/tst-strptime2.c
  2016-11-02 19:47 ` Steve Ellcey
@ 2016-11-02 20:55   ` Carlos O'Donell
  0 siblings, 0 replies; 3+ messages in thread
From: Carlos O'Donell @ 2016-11-02 20:55 UTC (permalink / raw)
  To: Steve Ellcey, libc-alpha

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-11-02 20:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-24 16:28 [PATCH] Fix -Wformat-length warning in time/tst-strptime2.c Steve Ellcey
2016-11-02 19:47 ` Steve Ellcey
2016-11-02 20:55   ` Carlos O'Donell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).