Bug 1098551 - gcc 8.1.1 prints incorrect warning for snprintf size
gcc 8.1.1 prints incorrect warning for snprintf size
Status: RESOLVED INVALID
Classification: openSUSE
Product: openSUSE Tumbleweed
Classification: openSUSE
Component: Development
Current
Other Other
: P5 - None : Normal (vote)
: ---
Assigned To: Richard Biener
E-mail List
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2018-06-21 08:10 UTC by Andreas Schneider
Modified: 2018-06-21 14:28 UTC (History)
4 users (show)

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Andreas Schneider 2018-06-21 08:10:47 UTC
gcc (SUSE Linux) 8.1.1 20180523 [gcc-8-branch revision 260570]

gcc -O3 -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.


#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;
}
Comment 1 Andreas Stieger 2018-06-21 14:15:49 UTC
I fixed a similar issue...
https://github.com/Microsoft/cpprestsdk/pull/787/files

(In reply to Andreas Schneider from comment #0)
> I don't really see how this can print 60 bytes as the integers are limited.

printf("02d", INT_MAX) is "2147483647" of length 10, times 6x %d is 60.

This warning is correct.
Comment 2 Richard Biener 2018-06-21 14:28:44 UTC
Thus invalid.