[Bug middle-end/86260] New: [8/9 Regression] incorrect -Wformat-truncation warning
dimhen at gmail dot com
gcc-bugzilla@gcc.gnu.org
Thu Jun 21 14:23:00 GMT 2018
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=86260
Bug ID: 86260
Summary: [8/9 Regression] incorrect -Wformat-truncation warning
Product: gcc
Version: 8.1.0
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: rguenth at gcc dot gnu.org
CC: dimhen at gmail dot com, msebor at gcc dot gnu.org
Target Milestone: ---
CC: dimhen at gmail dot com
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct octet_string {
char *data;
size_t length;
};
static int time2generalizedtime(time_t t, struct octet_string *s, int gtimep)
{
struct tm *tm = NULL;
const size_t len = gtimep ? 15 : 13;
s->data = malloc(len + 1);
if (s->data == NULL)
return ENOMEM;
s->length = len;
tm = gmtime(&t);
if (gtimep)
snprintf (s->data, len + 1, "%04d%02d%02d%02d%02d%02dZ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
else
snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
return 0;
}
int main(void)
{
struct octet_string data;
time_t now = time(0);
int rc;
rc = time2generalizedtime(now, &data, 0);
if (rc != 0) {
return 1;
}
return 0;
}
prints (also with -O2 or -O3):
gcc -O -Wall -Werror=format-truncation gcc_snprintf.c
gcc_snprintf.c: In function ‘main’:
gcc_snprintf.c:28:47: error: ‘%02d’ directive output may be truncated writing
between 2 and 11 bytes into a region of size between 0 and 10
[-Werror=format-truncation=]
snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
^~~~
gcc_snprintf.c:28:10: note: ‘snprintf’ output between 14 and 60 bytes into a
destination of size 14
snprintf (s->data, len + 1, "%02d%02d%02d%02d%02d%02dZ",
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tm->tm_year % 100, tm->tm_mon + 1, tm->tm_mday,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tm->tm_hour, tm->tm_min, tm->tm_sec);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc1: some warnings being treated as errors
I don't really see how this can print 60 bytes as the integers are limited.
More information about the Gcc-bugs
mailing list