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>
int cpu_mem_parser(char *data, const char **p, void **state)
{
int cpu_mem_parser(char *data, const char **p, void **state)
{
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");
static int make_uncached(char *path, size_t len)
{
static int make_uncached(char *path, size_t len)
{
char p1[1024], p2[1024], *p = path;
if (strstr(path, "/uncached/"))
char p1[1024], p2[1024], *p = path;
if (strstr(path, "/uncached/"))
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");