Patchwork [v1] Add Reiwa era tests to time/tst-strftime3.c

login
register
mail settings
Submitter DJ Delorie
Date April 3, 2019, 12:08 a.m.
Message ID <xnpnq4lzm6.fsf@greed.delorie.com>
Download mbox | patch
Permalink /patch/32133/
State New
Headers show

Comments

DJ Delorie - April 3, 2019, 12:08 a.m.
Steve Ellcey <sellcey@marvell.com> writes:
> I am building ToT glibc with ToT gcc and during glibc testing I got
> this error:

Yup, Arjun noticed as well when trying in Rawhide.  It's a wierd
warning, too, because the *point* of snprintf is that it truncates the
output.

Arjun and I talked about a few options, but I think the simplest is to
just shut gcc up...  alternatives include far bigger buffers with
sprintf, or xasprintf... but what's the point of snprintf at all then?
Andreas Schwab - April 3, 2019, 7:08 a.m.
On Apr 02 2019, DJ Delorie <dj@redhat.com> wrote:

> Steve Ellcey <sellcey@marvell.com> writes:
>> I am building ToT glibc with ToT gcc and during glibc testing I got
>> this error:
>
> Yup, Arjun noticed as well when trying in Rawhide.  It's a wierd
> warning, too, because the *point* of snprintf is that it truncates the
> output.
>
> Arjun and I talked about a few options, but I think the simplest is to
> just shut gcc up...  alternatives include far bigger buffers with
> sprintf, or xasprintf... but what's the point of snprintf at all then?
>
>
> diff --git a/time/tst-strftime3.c b/time/tst-strftime3.c
> index 32ce0d93e2..2a4c3ea398 100644
> --- a/time/tst-strftime3.c
> +++ b/time/tst-strftime3.c
> @@ -430,16 +430,21 @@ static void
>  tm_to_printed (struct tm *tm, char *buffer)
>  {
>    const char *wn;
> -  char temp[50];
> +  char temp[28];
>  
>    if (0 <= tm->tm_wday && tm->tm_wday <= 6)
>      wn = weekday_name[tm->tm_wday];

Does it change anything if you make weekday_name const or static?

Andreas.
Andreas Schwab - April 3, 2019, 7:13 a.m.
On Apr 02 2019, DJ Delorie <dj@redhat.com> wrote:

> Yup, Arjun noticed as well when trying in Rawhide.  It's a wierd
> warning, too, because the *point* of snprintf is that it truncates the
> output.

The point of -Wformat-truncation is to warn if snprintf results in
incomplete output, which is a generally a bug.  If you want to truncate
a string argument, use %s with a precision.

Andreas.

Patch

diff --git a/time/tst-strftime3.c b/time/tst-strftime3.c
index 32ce0d93e2..2a4c3ea398 100644
--- a/time/tst-strftime3.c
+++ b/time/tst-strftime3.c
@@ -430,16 +430,21 @@  static void
 tm_to_printed (struct tm *tm, char *buffer)
 {
   const char *wn;
-  char temp[50];
+  char temp[28];
 
   if (0 <= tm->tm_wday && tm->tm_wday <= 6)
     wn = weekday_name[tm->tm_wday];
   else
     {
       wn = temp;
+      /* 64-bit values need at most 21 bytes, including NUL.  */
       sprintf (temp, "%d", tm->tm_wday);
     }
 
+  /* This will be at most 4+2+2+2+2+2+7+20 = 41 bytes long, including
+     NUL.  GCC warns if snprintf may truncate the output, which is why
+     we're using snprintf...  */
+#pragma GCC diagnostic ignored "-Wformat-truncation="
   snprintf (buffer, TMBUFLEN, "%04d/%02d/%02d %02d:%02d:%02d %s",
 	    tm->tm_year + 1900,
 	    tm->tm_mon + 1,