diff mbox series

Silent -Wformat-truncation warnings in date_and_time.c.

Message ID 7882cc27-cff0-71a8-269f-88bfa7836147@suse.cz
State New
Headers show
Series Silent -Wformat-truncation warnings in date_and_time.c. | expand

Commit Message

Martin Liška May 3, 2019, 10:53 a.m. UTC
Hi.

The patch is about suppression of the following warning:

/home/gcc/buildworker/zenith-gcc-trunk-weekly/build/libgfortran/intrinsics/date_and_time.c:165:33: warning: ‘%04d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 9 [-Wformat-truncation=]
/home/gcc/buildworker/zenith-gcc-trunk-weekly/build/libgfortran/intrinsics/date_and_time.c:172:33: warning: ‘%+03d’ directive output may be truncated writing between 3 and 9 bytes into a region of size 6 [-Wformat-truncation=]

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

libgfortran/ChangeLog:

2019-05-03  Martin Liska  <mliska@suse.cz>

	* intrinsics/date_and_time.c (DATE_LEN): Enlarge in order to
	deal with the warning.
	(ZONE_LEN): Likewise.
---
 libgfortran/intrinsics/date_and_time.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Steve Kargl May 3, 2019, 3:42 p.m. UTC | #1
On Fri, May 03, 2019 at 12:53:06PM +0200, Martin Liška wrote:
> 
> The patch is about suppression of the following warning:
> 
> /home/gcc/buildworker/zenith-gcc-trunk-weekly/build/libgfortran/intrinsics/date_and_time.c:165:33: warning: ‘%04d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 9 [-Wformat-truncation=]
> /home/gcc/buildworker/zenith-gcc-trunk-weekly/build/libgfortran/intrinsics/date_and_time.c:172:33: warning: ‘%+03d’ directive output may be truncated writing between 3 and 9 bytes into a region of size 6 [-Wformat-truncation=]
> 
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
> 
> Ready to be installed?

Why are you getting a warning?  Is this a wchar issue?

The comment in the code for DATE is 

   DATE (optional) shall be scalar and of type default character.
   It is an INTENT(OUT) argument.  It is assigned a value of the
   form CCYYMMDD, where CC is the century, YY the year within the
   century, MM the month within the year, and DD the day within the
   month.  If there is no date available, they are assigned blanks.

12345678
CCYYMMDD

That is 8 characters, so 

#define DATE_LEN 8
...
  char date[DATE_LEN + 1];

would appear to be correct.  The '+ 1' is for the terminating '\0'.

Futhermore, 'date' appears as an argument to snprintf() and
memset(), where both function will write at most DATE_LEN
characters into 'date'.

Arbitrarily, increasing the sizes of 'date' and 'zone' to
silence a bogus warning seems dubious to me.  Remove the
-W option if the false positive offends you.
diff mbox series

Patch

diff --git a/libgfortran/intrinsics/date_and_time.c b/libgfortran/intrinsics/date_and_time.c
index d536404a214..ff7204762da 100644
--- a/libgfortran/intrinsics/date_and_time.c
+++ b/libgfortran/intrinsics/date_and_time.c
@@ -117,9 +117,9 @@  gmtime_r (const time_t * timep, struct tm * result)
    TODO :
    - Check year boundaries.
 */
-#define DATE_LEN 8
+#define DATE_LEN 24
 #define TIME_LEN 10   
-#define ZONE_LEN 5
+#define ZONE_LEN 11
 #define VALUES_SIZE 8
 
 extern void date_and_time (char *, char *, char *, gfc_array_i4 *,