emilk / loguru

A lightweight C++ logging library
The Unlicense
1.77k stars 256 forks source link

Many `snprintf` truncation warnings #184

Open kcgen opened 2 years ago

kcgen commented 2 years ago

GCC -Wformat-truncation=2 flag warns about potential truncations on the source content.

It flags many warnings in Loguru:

[108/305] Compiling C++ object src/libs/loguru/libloguru.a.p/loguru.cpp.o
../../src/libs/loguru/loguru.cpp: In function ‘void loguru::print_preamble_header(char*, size_t)’:
../../src/libs/loguru/loguru.cpp:1259:85: warning: ‘time         ’ directive output truncated writing 13 bytes into a region of size 1 [-Wformat-truncation=]
 1259 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "time         ");
      |                                                                                    ~^~~~~~~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output 14 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1265:85: warning: ‘( uptime  ) ’ directive output truncated writing 12 bytes into a region of size 1 [-Wformat-truncation=]
 1265 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "( uptime  ) ");
      |                                                                                    ~^~~~~~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output 13 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1271:85: warning: ‘%-*s’ directive output truncated writing 16 bytes into a region of size 0 [-Wformat-truncation=]
 1271 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]", LOGURU_THREADNAME_WIDTH, " thread name/id");
      |                                                                                     ^~~~                             ~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output 19 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1277:84: warning: ‘%*s’ directive output truncated writing 23 bytes into a region of size 1 [-Wformat-truncation=]
 1277 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:line  ", LOGURU_FILENAME_WIDTH, "file");
      |                                                                                    ^~~                                 ~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output 31 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1283:85: warning: ‘   v’ directive output truncated writing 4 bytes into a region of size 1 [-Wformat-truncation=]
 1283 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "   v");
      |                                                                                    ~^~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output 5 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1289:79: warning: ‘| ’ directive output truncated writing 2 bytes into a region of size 1 [-Wformat-truncation=]
 1289 |                         (void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
      |                                                                              ~^
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output 3 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp: In function ‘void loguru::get_thread_name(char*, long long unsigned int, bool)’:
../../src/libs/loguru/loguru.cpp:1068:82: warning: ‘__builtin_snprintf’ output may be truncated before the last format character [-Wformat-truncation=]
 1068 |                                 snprintf(buffer, static_cast<size_t>(length), "%s", name);
      |                                                                                  ^
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin_snprintf’ output 1 or more bytes (assuming 2) into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1107:80: warning: ‘%X’ directive output may be truncated writing between 1 and 8 bytes into a region of size 1 [-Wformat-truncation=]
 1107 |                                 snprintf(buffer, static_cast<size_t>(length), "%X", static_cast<unsigned>(thread_id));
      |                                                                                ^~
../../src/libs/loguru/loguru.cpp:1107:79: note: using the range [0, 4294967295] for directive argument
 1107 |                                 snprintf(buffer, static_cast<size_t>(length), "%X", static_cast<unsigned>(thread_id));
      |                                                                               ^~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 2 and 9 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1105:80: warning: ‘%*X’ directive output may be truncated writing between 1 and 2147483648 bytes into a region of size 1 [-Wformat-truncation=]
 1105 |                                 snprintf(buffer, static_cast<size_t>(length), "%*X", static_cast<int>(length - 1), static_cast<unsigned>(thread_id));
      |                                                                                ^~~
../../src/libs/loguru/loguru.cpp:1105:79: note: assuming directive output of 9 bytes
 1105 |                                 snprintf(buffer, static_cast<size_t>(length), "%*X", static_cast<int>(length - 1), static_cast<unsigned>(thread_id));
      |                                                                               ^~~~~
../../src/libs/loguru/loguru.cpp:1105:79: note: using the range [0, 4294967295] for directive argument
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 2 and 2147483649 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp: In function ‘void loguru::print_preamble(char*, size_t, loguru::Verbosity, const char*, unsigned int)’:
../../src/libs/loguru/loguru.cpp:1325:71: warning: ‘% 4d’ directive output may be truncated writing between 4 and 11 bytes into a region of size 5 [-Wformat-truncation=]
 1325 |                         snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                                       ^~~~
../../src/libs/loguru/loguru.cpp:1325:70: note: directive argument in the range [1, 2147483647]
 1325 |                         snprintf(level_buff, sizeof(level_buff) - 1, "% 4d", verbosity);
      |                                                                      ^~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 5 and 12 bytes into a destination of size 5
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1338:84: warning: ‘%02d’ directive output truncated writing between 2 and 11 bytes into a region of size 1 [-Wformat-truncation=]
 1338 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
      |                                                                                    ^~~~
../../src/libs/loguru/loguru.cpp:1338:83: note: directive argument in the range [-999, 999]
 1338 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%02d:%02d:%02d.%03lld ",
      |                                                                                   ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 14 and 42 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1345:85: warning: ‘%8.3f’ directive output truncated writing between 8 and 314 bytes into a region of size 0 [-Wformat-truncation=]
 1345 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "(%8.3fs) ",
      |                                                                                     ^~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 13 and 319 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1352:85: warning: ‘%-*s’ directive output truncated writing 16 or more bytes into a region of size 0 [-Wformat-truncation=]
 1352 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "[%-*s]",
      |                                                                                     ^~~~
 1353 |                                              LOGURU_THREADNAME_WIDTH, thread_name);
      |                                                                       ~~~~~~~~~~~    
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output 19 or more bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1361:84: warning: ‘%*s’ directive output truncated writing 23 bytes into a region of size 1 [-Wformat-truncation=]
 1361 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%*s:%-5u ",
      |                                                                                    ^~~
 1362 |                                              LOGURU_FILENAME_WIDTH, shortened_filename, line);
      |                                                                     ~~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 31 and 36 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1368:84: warning: ‘%4s’ directive output truncated writing between 4 and 5 bytes into a region of size 1 [-Wformat-truncation=]
 1368 |                         int bytes = snprintf(out_buff + pos, out_buff_size - pos, "%4s",
      |                                                                                    ^~~
 1369 |                                              level_buff);
      |                                              ~~~~~~~~~~                             
In file included from /usr/include/stdio.h:866,
                 from /usr/include/c++/11/cstdio:42,
                 from ../../src/libs/loguru/loguru.cpp:38:
/usr/include/x86_64-linux-gnu/bits/stdio2.h:71:35: note: ‘__builtin___snprintf_chk’ output between 5 and 6 bytes into a destination of size 1
   71 |   return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1,
      |          ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   72 |                                    __glibc_objsize (__s), __fmt,
      |                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   73 |                                    __va_arg_pack ());
      |                                    ~~~~~~~~~~~~~~~~~
../../src/libs/loguru/loguru.cpp:1375:79: warning: ‘| ’ directive output truncated writing 2 bytes into a region of size 1 [-Wformat-truncation=]
 1375 |                         (void)snprintf(out_buff + pos, out_buff_size - pos, "| ");
kcgen commented 2 years ago

I am happy to submit a PR to address these - however, I would like to know before-hand if this is something the author is interested in reviewing an accepting?

The reason I ask is because I have submitted a prior PR that fixes all known static analysis issues in Loguru (https://github.com/emilk/loguru/pull/179/files), which is in-sync w/ latest and passes CI test but has sat two months without a response.

Thanks for your consideration.