]> git.itanic.dy.fi Git - rrdd/commitdiff
Silence gcc-8 snprintf overflow warnings
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Fri, 7 Sep 2018 18:16:27 +0000 (21:16 +0300)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Fri, 7 Sep 2018 18:21:14 +0000 (21:21 +0300)
When compilint with gcc-8 and newer, we get warnings like:

/usr/include/bits/stdio2.h:64:10: note: ‘__builtin___snprintf_chk’ output between 11 and 2057 bytes into a destination of size 1024
   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        __bos (__s), __fmt, __va_arg_pack ());
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is because gcc thinks (correctly) that it is possible for the
resulting string to be larger than the buffer we ask snprintf to print
it. This is pretty bening warning, as we are not expecting to see this
kind of situations ever.

To work around the warning, add an error print for such cases when
actual overflow takes place. No actual memory overflow takes place
however as snprintf prevents this. But this also makes gcc happy as we
are now dealing the error case.

Signed-off-by: Timo Kokkonen <timo.t.kokkonen@iki.fi>
built_in_parsers.c
onewire_parser.c

index bf65ee9ad62006ee69b21fce0ec80d4ed95125cf..c18c080e972283dd4de30a3916fea78630089076 100644 (file)
@@ -108,11 +108,16 @@ static int mem_parser(char *data, const char **p, void **state)
 
 int cpu_mem_parser(char *data, const char **p, void **state)
 {
 
 int cpu_mem_parser(char *data, const char **p, void **state)
 {
+       int ret;
        char cpu[RRD_DATA_MAX_LEN], mem[RRD_DATA_MAX_LEN];
 
        cpu_parser(cpu, p, state);
        mem_parser(mem, p, state);
        char cpu[RRD_DATA_MAX_LEN], mem[RRD_DATA_MAX_LEN];
 
        cpu_parser(cpu, p, state);
        mem_parser(mem, p, state);
-       snprintf(data, RRD_DATA_MAX_LEN, "%s:%s", cpu, mem);
+       ret = snprintf(data, RRD_DATA_MAX_LEN, "%s:%s", cpu, mem);
+
+       /* No actual data overflow, snprintf just couldn't fit all data in the buffer */
+       if (ret >= RRD_DATA_MAX_LEN)
+               pr_err("Buffer overlfow\n");
 
        return 0;
 }
 
        return 0;
 }
index decc133e940eda7fcabc8bfc9d7ef5121061af0a..7d1e8a6e3d7eb3fbe6b91643c363a99e699524ef 100644 (file)
@@ -124,6 +124,7 @@ static int parse_opts(const char *str, char *ow_path, size_t pathlen,
 
 static int make_uncached(char *path, size_t len)
 {
 
 static int make_uncached(char *path, size_t len)
 {
+       int ret;
        char p1[1024], p2[1024], *p = path;
 
        if (strstr(path, "/uncached/"))
        char p1[1024], p2[1024], *p = path;
 
        if (strstr(path, "/uncached/"))
@@ -147,7 +148,11 @@ static int make_uncached(char *path, size_t len)
 
        strncpy(p1, path, sizeof(p1) - 1);
        strncpy(p2, p, sizeof(p2) - 1);
 
        strncpy(p1, path, sizeof(p1) - 1);
        strncpy(p2, p, sizeof(p2) - 1);
-       snprintf(path, len, "%s/uncached/%s", p1, p2);
+       ret = snprintf(path, len, "%s/uncached/%s", p1, p2);
+
+       /* No actual data overflow, snprintf just couldn't fit all data in the buffer */
+       if (ret >= RRD_DATA_MAX_LEN)
+               pr_err("Buffer overlfow\n");
 
        return 0;
 }
 
        return 0;
 }