From: Timo Kokkonen Date: Fri, 7 Sep 2018 18:16:27 +0000 (+0300) Subject: Silence gcc-8 snprintf overflow warnings X-Git-Url: http://git.itanic.dy.fi/?p=rrdd;a=commitdiff_plain;h=5c4ca53b276c6670da6e143ef4b161e1f91cf205 Silence gcc-8 snprintf overflow warnings 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 --- diff --git a/built_in_parsers.c b/built_in_parsers.c index bf65ee9..c18c080 100644 --- a/built_in_parsers.c +++ b/built_in_parsers.c @@ -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 ret; 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; } diff --git a/onewire_parser.c b/onewire_parser.c index decc133..7d1e8a6 100644 --- a/onewire_parser.c +++ b/onewire_parser.c @@ -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) { + int ret; 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); - 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; }