Home GnuPG

yat2m: Avoid compiler warnings.

Description

yat2m: Avoid compiler warnings.

* doc/yat2m.c (isodatestring): Prepare enough buffer.
  • Signed-off-by: NIIBE Yutaka <gniibe@fsij.org>

Details

Provenance
gniibeAuthored on Oct 22 2018, 3:28 AM
Parents
rE91bcb2c7d824: gpg-error-config: Support "auto", architecture independent script.
Branches
Unknown
Tags
Unknown

Event Timeline

Can you please be so kind and show the compiler warning? I can't see any fault in the code unless the OS is broken.
Instead of that change I would suggest to change the sprintf to

snprintf (buffer,  sizeof buffer, "%04d-%02d-%02d",
          1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday )

I guess that I used sprintf for increased portability but meanwhile snprintf is available on all platforms.

Here is the warning:

../../libgpg-error/doc/yat2m.c: In function 'isodatestring':
../../libgpg-error/doc/yat2m.c:373:29: warning: '%02d' directive writing between 2 and 11 bytes into a region of size between 4 and 11 [-Wformat-overflow=]
       sprintf (buffer,"%04d-%02d-%02d",
                             ^~~~
../../libgpg-error/doc/yat2m.c:373:23: note: directive argument in the range [-2147483647, 2147483647]
       sprintf (buffer,"%04d-%02d-%02d",
                       ^~~~~~~~~~~~~~~~
../../libgpg-error/doc/yat2m.c:373:7: note: 'sprintf' output between 11 and 36 bytes into a destination of size 16
       sprintf (buffer,"%04d-%02d-%02d",
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

True, "integer" can be larger, from the viewpoint of compiler.

(Change to snprintf doesn't suppress this warning.)

While we have no way to express possible range of values in structure member, we can do something like this (which is not needed actually, if the gmtime function returns correct value).

@@ -370,8 +370,8 @@ isodatestring (void)
   else
     {
       tp = gmtime (&atime);
-      sprintf (buffer,"%04d-%02d-%02d",
-               1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday );
+      sprintf (buffer, "%04d-%02d-%02d", (1900U+tp->tm_year)%10000,
+	       (tp->tm_mon+1U)%100, (tp->tm_mday+0U)%100);
     }
   return buffer;
 }